gpt4 book ai didi

java - JAVA中老虎机无限循环问题

转载 作者:太空宇宙 更新时间:2023-11-04 10:18:34 25 4
gpt4 key购买 nike

我正在编写一个基本的老虎机代码,当程序运行时它似乎陷入了无限循环。如果有人可以阐明此错误的位置,或者您会推荐一些内容。感谢所有的帮助。

public class Java_Lab_3 {
public static void main(String[] args) {
int endgame;
int slot1;
int slot2;
int slot3;

Random rand = new Random();
Scanner keyboard = new Scanner(System.in);

System.out.println("Want to test your luck?");
System.out.println("To Spin, type any positive number");
System.out.println("To End Game, type -1 and press enter ");

endgame = keyboard.nextInt();

while (endgame != -1) {
slot1 = rand.nextInt(10);
slot2 = rand.nextInt(10);
slot3 = rand.nextInt(10);

System.out.println(slot1 + slot2 + slot3);
System.out.println(endgame);

if (slot1 == slot2 && slot1 == slot3) { // Then they have JACKPOT
System.out.println("You've Won!");
} else if (slot1 == slot2 || slot2 == slot3 || slot1 == slot3) { // They MATCHED 2
System.out.println("We have twins... You've matched a pair!");
} else {
System.out.println("Sucks to Suck, don't quit your day job!");
}
}
}
}

最佳答案

如果您想跳出该循环,则必须编写退出条件的可能性。例如...

while (endgame != -1) {
System.out.print("Do you want to play again? (-1 to quit): ");
endgame = keyboard.nextInt();
}

通过这种方式,您可以在每次掷骰后提示用户再次玩游戏。

关于java - JAVA中老虎机无限循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51412491/

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