试图弄清楚如何打印所有正确答案的总分,非常基本,还试图包含布尔重复功能,这样我就可以要求用户再次播放"或者"退出"。
import java.util.Scanner;
import java.util.Random;
public class GameVer1 {
public static void main(String []args) {
String inputName;
System.out.print("Enter your name here...");
inputName = userInput.nextLine();
System.out.print("Hi "+ inputName + ", Welcome!");
//boolean repeat = true;
int tries = 0;
int score = 0;
int count = 0;
do{
Scanner userInput = new Scanner(System.in);
Random randomNumberChooser = new Random();
int ranNum1;
ranNum1 = randomNumberChooser.nextInt(12)+2;
int ranNum2;
ranNum2 = randomNumberChooser.nextInt(12)+2;
int response = (ranNum1 * ranNum2);
String question;
System.out.println("How much is " + ranNum1 +" x " + ranNum2 + "?");
question = userInput.next();
tries = tries + 1;
if(userInput == response){
System.out.print("Correct!");
userInput = response;
count = count + 1;
}
}while(tries <10);
}
}
答案 0 :(得分:1)
您需要先将输入转换为int
进行比较,如下所示:
int num = userInput.nextInt();
然后,您就可以比较if (num == response) {
,您将不会再收到错误。
修改强>
另外,据我所知,您可以删除行userInput = response;
,因为它也会给您一个错误,但即便如此,userInput
会在下一次迭代时再次读取循环。