gpt4 book ai didi

java - 简单的java乘法游戏

转载 作者:行者123 更新时间:2023-12-02 11:20:20 25 4
gpt4 key购买 nike

我正在制作一个简单的乘法游戏,但是当用户输入正确答案时,它仍然运行 else 语句。我知道这是一个简单的解决方案,但我就是想不出来。有人可以帮我吗?

public static void partB() {
System.out.println("Exercise 1B");
int count = 0;
String active = "true";
int correct = 0;
while (active == "true") {
Random r = new Random();
int Low = 10; //inclusive
int High = 21; //not inclusive
int Result = r.nextInt(High - Low) + Low;
Random r2 = new Random();
int Result2 = r2.nextInt(High - Low) + Low;
int Total = Result * Result2;
Scanner input = new Scanner(System.in);
System.out.println(Result + "*" + Result2 + "=?");
String guess = new String(input.nextLine());
String tostrng = String.valueOf(Total);

if (guess.equals (tostrng)) {
correct += 1;
count+=1;
System.out.println("Correct answer. Score: " + correct + "(" + count + ")");
}
if (guess.equals("q")) {
System.out.println("Good Bye!");
active = "false";
//return;
}
else {
count +=1;
System.out.println("Incorrect answer. Score:" + correct + "(" + count + ")");
}
}
}

最佳答案

你的 else 与 if 错误

public static void partB() {
System.out.println("Exercise 1B");
int count = 0;
String active = "true";
int correct = 0;
while (active == "true") {
Random r = new Random();
int Low = 10; //inclusive
int High = 21; //not inclusive
int Result = r.nextInt(High - Low) + Low;
Random r2 = new Random();
int Result2 = r2.nextInt(High - Low) + Low;
int Total = Result * Result2;
Scanner input = new Scanner(System.in);
System.out.println(Result + "*" + Result2 + "=?");
String guess = new String(input.nextLine());
String tostrng = String.valueOf(Total);

if (guess.equals (tostrng)) {
correct += 1;
count+=1;
System.out.println("Correct answer. Score: " + correct + "(" + count + ")");
}
else {
count +=1;
System.out.println("Incorrect answer. Score:" + correct + "(" + count + ")");
}
if (guess.equals("q")) {
System.out.println("Good Bye!");
active = "false";
//return;
}
}
}

关于java - 简单的java乘法游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49966031/

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