gpt4 book ai didi

java - 使用随机整数询问乘法表

转载 作者:行者123 更新时间:2023-12-01 13:50:31 26 4
gpt4 key购买 nike

我编写了以下代码,询问“num1 乘以 num2 是多少?”这个问题。但是,当我尝试运行 java 文件时,我没有得到任何响应。你能帮助我理解我做错了什么吗?代码如下:

import java.util.Scanner;
import java.util.Random;

public class MultiplyLearn{

public void Learn(){

Random multiple = new Random();
Scanner input = new Scanner( System.in );
boolean wrong = true;

int num1 = 1 + multiple.nextInt( 9 );
int num2 = 1 + multiple.nextInt( 9 );

while( wrong == true ){

askQuestion( num1, num2 );
int answer = input.nextInt();

if( answer == num1*num2 ){
System.out.println( "Very Good" );
wrong = false;
}

else{
System.out.print( "No. Please try again." );
}
}
}

public String askQuestion( int x, int y ){

return "How much is" + x + "times" + y + "?";
}
}

最佳答案

向类中添加一个 main 方法

import java.util.Scanner;
import java.util.Random;

public class MultiplyLearn{

//your actual code goes here

public static void main(String args[]) throws Exception{
new MultiplyLearn().Learn();
}
}

所以你的最后一个类将如下所示

import java.util.Scanner;
import java.util.Random;

public class MultiplyLearn{

public void Learn(){

Random multiple = new Random();
Scanner input = new Scanner( System.in );
boolean wrong = true;

int num1 = 1 + multiple.nextInt( 9 );
int num2 = 1 + multiple.nextInt( 9 );

while( wrong == true ){

askQuestion( num1, num2 );
int answer = input.nextInt();

if( answer == num1*num2 ){
System.out.println( "Very Good" );
wrong = false;
}

else{
System.out.print( "No. Please try again." );
}
}
}

public String askQuestion( int x, int y ){

return "How much is" + x + "times" + y + "?";
}

public static void main(String args[]) throws Exception{
new MultiplyLearn().Learn();
}
}

关于java - 使用随机整数询问乘法表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19996883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com