gpt4 book ai didi

Java得到正确答案+ boolean 重复的最终分数

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:46 26 4
gpt4 key购买 nike

尝试找出如何打印所有正确答案的总分,非常基本,还尝试包含 boolean 重复函数,以便我可以要求用户“再次玩”或“退出”。

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);
}

}

最佳答案

您需要首先将输入转换为 int 进行比较,如下所示:

int num = userInput.nextInt();

然后您将能够比较 if (num == response) { 并且您将不再收到错误。

编辑

此外,据我所知,您可以删除 userInput = response; 行,因为它也会给您带来错误,但即使如此,在循环的下一次迭代时也会再次读取 userInput

关于Java得到正确答案+ boolean 重复的最终分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28998049/

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