gpt4 book ai didi

Java while 循环不会中断(我也将条件设置为 false)

转载 作者:行者123 更新时间:2023-11-30 06:06:48 27 4
gpt4 key购买 nike

我做了一个东西,可以掷五个骰子,直到得到五颗骰子。我将在下面粘贴我的代码,以便您可以看到它。如果五对一成功,我已将 while 条件设置为 false。当它说“滚动...”后什么也没有发生。我应该使用 .equals() 吗?

package omega;

import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class One {

public static void main(String[] args) throws InterruptedException {
Random random = new Random();
Scanner scan = new Scanner(System.in);

int one = 1+random.nextInt(6);
int two = 1+random.nextInt(6);
int three = 1+random.nextInt(6);
int four = 1+random.nextInt(6);
int five = 1+random.nextInt(6);

boolean rolling = true;
int rollCount = 0;

System.out.println("====================");
System.out.println(" Yahtzee! ");
System.out.println("====================");
System.out.println("");


System.out.println("Type 0 to roll.");
int roll = scan.nextInt();

System.out.println("Rolling...");

while(rolling == true) {
switch(roll) {
case 0 :
if(two == one && three == one && four == one) {
rollCount++;
rolling = false;
}
else {
rollCount++;
}
break;
default :
System.out.println("Invalid roll!");
}
}

System.out.println("Yahtzee!");
System.out.println("Took " + rollCount + " rolls!");



}


}

最佳答案

您没有在循环内重新滚动。如果roll不为0,它将是一个无限循环,打印“Invalid roll!”一遍又一遍。

关于Java while 循环不会中断(我也将条件设置为 false),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43379841/

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