gpt4 book ai didi

java - 有人可以帮我这个java代码吗?循环错误

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

我对编程很陌生,正在尝试制作更高或更低的游戏,但遇到了一些问题。首先,我希望程序在猜对卡片时循环,即选择较低的选项并且它较低。然而,它在第二次循环后崩溃并且没有任何反应。当游戏结束时,我会并选择再次玩,但什么也没有发生。如果有人可以帮助我解决这些问题,我将不胜感激。谢谢

import

最佳答案

试试这个:

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int card = 0;
int cardNext;

String userResponse;
String userPlayAgain;
boolean userGuessedRight = true;

do {
do {
Random randomNumbers = new Random();
card = randomNumbers.nextInt(13 - 1 + 1) + 1;
System.out.println("Card is " + card);

System.out.print("Higher or Lower?");
cardNext = randomNumbers.nextInt(13) + 1;
userResponse = scan.next();
userGuessedRight = (userResponse.equalsIgnoreCase("H") && cardNext >= card)
|| (userResponse.equalsIgnoreCase("L") && cardNext <= card);

if (userGuessedRight) {
System.out.println("Card is " + cardNext);
System.out.print("Higher or Lower?");
userResponse = scan.next();
} else {
break;
}
} while (userResponse.equalsIgnoreCase("H") && cardNext < card || userResponse.equalsIgnoreCase("L") && cardNext > card);
System.out.println("Card is " + cardNext);
System.out.println("Card is higher/lower you lose – play again? Y/N : ");
userPlayAgain = scan.next();
} while (userPlayAgain.equalsIgnoreCase("Y"));

do {
if (userPlayAgain.equalsIgnoreCase("N"))

{
System.out.print("Thank You.");
break;
}

} while (!userPlayAgain.equalsIgnoreCase("Y"));

scan.close();
}

编辑

这是输出: The output of the program

关于java - 有人可以帮我这个java代码吗?循环错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27156079/

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