我的程序应该在三个骰子都掷出六个骰子之前打印出掷骰子的数量。
这就是我到目前为止所得到的,但我得到的输出是像1这样的小数字,我不认为需要1个时间来滚动所有的3个6。我期待更大的数字。
Random rand = new Random();
int numOfRolls = 0; //starts at zero for the number of rolls
int x;
int y;
int z;
do {
numOfRolls++;
x = rand.nextInt(6) + 1;
y = rand.nextInt(6) + 1;
z = rand.nextInt(6) + 1;
} while (x == 6 || y == 6 || z == 6);
System.out.println(numOfRolls);发布于 2017-10-09 08:30:45
将while条件更改为:
while (x !=6 || y != 6 || z != 6)这将导致循环继续,直到所有三个值都为6。
发布于 2017-10-09 08:27:15
使用or's将告诉您三个整数中的一个落在6上之前的滚动数。此外,您还希望程序在落在三个6上时停止,因此将==切换为!=。
x != 6 && y != 6 && z != 6发布于 2017-10-09 08:30:19
逻辑条件需要满足:
while(x != 6 || y != 6 || z != 6)
因为您需要重复循环,直到所有三个循环都不是6
示例:
Random rand = new Random();
int numOfRolls = 0; //starts at zero for the number of rolls
int x;
int y;
int z;
do {
numOfRolls++;
x = rand.nextInt(6) + 1;
y = rand.nextInt(6) + 1;
z = rand.nextInt(6) + 1;
} while (x != 6 || y != 6 || z != 6);
System.out.println(numOfRolls);https://stackoverflow.com/questions/46637335
复制相似问题