我正在编写一个基本的老虎机代码,当程序运行时它似乎陷入了无限循环。如果有人可以阐明此错误的位置,或者您会推荐一些内容。感谢所有的帮助。
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();
}
通过这种方式,您可以在每次掷骰后提示用户再次玩游戏。
我是一名优秀的程序员,十分优秀!