骰子加倍Java练习-逻辑错误
我是一个通过在线资源自学Java的初学者。我遇到了这个练习 编写一个程序,通过从1-6中选择一个随机数,然后从1-6中选择第二个随机数来模拟掷骰子。将这两个值相加,并显示总数。修改您的骰子游戏,使其保持滚动,直到他们得到双倍(两个骰子上的数字相同) 到目前为止,我有以下代码:骰子加倍Java练习-逻辑错误,java,Java,我是一个通过在线资源自学Java的初学者。我遇到了这个练习 编写一个程序,通过从1-6中选择一个随机数,然后从1-6中选择第二个随机数来模拟掷骰子。将这两个值相加,并显示总数。修改您的骰子游戏,使其保持滚动,直到他们得到双倍(两个骰子上的数字相同) 到目前为止,我有以下代码: public class DiceGame { public static void main(String[] args) { System.out.println("ROLL THE DICE!\n");
public class DiceGame {
public static void main(String[] args) {
System.out.println("ROLL THE DICE!\n");
int firstRoll = 1 + (int) (Math.random() * 6);
int secondRoll = 1 + (int) (Math.random() * 6);
while (firstRoll != secondRoll) {
System.out.println("Roll #1: " + firstRoll);
System.out.println("Roll #2: " + secondRoll);
int total = firstRoll + secondRoll;
System.out.println("The total is " + total);
}
System.out.println("You rolled doubles!");
System.out.println("Roll #1: " + firstRoll);
System.out.println("Roll #2: " + secondRoll);
int total = firstRoll + secondRoll;
System.out.println("The total is " + total);
}
}
问题是,当我运行这段代码时,如果两个卷不相同,程序将永远运行,输出相同的第一卷和第二卷值以及总计……我确信while循环存在逻辑错误。请帮忙
以下是我的输出示例:
Roll #1: 2
Roll #2: 3
The total is 5
Roll #1: 2
Roll #2: 3
The total is 5
Roll #1: 2
Roll #2: 3
The total is 5
Roll #1: 2
Roll #2: 3
The total is 5
Roll #1: 2
Roll #2: 3
The total is 5
Roll #1: 2
Roll #2: 3
The total is 5
Roll #1: 2
Roll #2: 3
The total is 5
Roll #1: 2
Roll #2: 3
The total is 5
Roll #1: 2
Roll #2: 3
The total is 5
(while条件不断返回false并打印相同的值)
以下是所需输出的示例:
Roll #1: 3
Roll #2: 5
The total is 8
Roll #1: 6
Roll #2: 1
The total is 7
Roll #1: 2
Roll #2: 5
The total is 7
Roll #1: 1
Roll #2: 1
The total is 2
(程序应在翻滚双打时结束)您需要在每次迭代时再次翻滚:
while (firstRoll != secondRoll) {
System.out.println("Roll #1: " + firstRoll);
System.out.println("Roll #2: " + secondRoll);
int total = firstRoll + secondRoll;
System.out.println("The total is " + total);
firstRoll = 1 + (int) (Math.random() * 6);
secondRoll = 1 + (int) (Math.random() * 6);
}
您需要在每次迭代时再次进行滚动:
while (firstRoll != secondRoll) {
System.out.println("Roll #1: " + firstRoll);
System.out.println("Roll #2: " + secondRoll);
int total = firstRoll + secondRoll;
System.out.println("The total is " + total);
firstRoll = 1 + (int) (Math.random() * 6);
secondRoll = 1 + (int) (Math.random() * 6);
}
如果它们的值不一样,你必须再次掷骰子 在这种情况下,使用
do而是很好的
int firstRoll, secondRoll;
do {
firstRoll = 1 + (int) (Math.random() * 6);
secondRoll = 1 + (int) (Math.random() * 6);
System.out.println("Roll #1: " + firstRoll);
System.out.println("Roll #2: " + secondRoll);
int total = firstRoll + secondRoll;
System.out.println("The total is " + total);
} while (firstRoll != secondRoll);
如果它们的值不一样,你必须再次掷骰子
在这种情况下,使用do而是很好的
int firstRoll, secondRoll;
do {
firstRoll = 1 + (int) (Math.random() * 6);
secondRoll = 1 + (int) (Math.random() * 6);
System.out.println("Roll #1: " + firstRoll);
System.out.println("Roll #2: " + secondRoll);
int total = firstRoll + secondRoll;
System.out.println("The total is " + total);
} while (firstRoll != secondRoll);
编写一个方法public int roll()
,并将计算1-6之间随机数的逻辑放在那里。您只是输出firstRoll和secondRoll的结果,而不是在方法中再次计算
例如:
public class Dice() {
public int roll() {
return 1 + (int) (Math.random() * 6);
}
}
并使用它:
Dice d1 = new Dice();
Dice d2 = new Dice();
int d1Result = d1.roll();
int d1Result = d2.roll();
while (d1Result != d2Result) {
d1Result = d1.roll();
d2Result = d2.roll();
System.out.println("Result d1: " + d1Result);
System.out.println("Result d2: " + d2Result);
// Rest of logic ...
}
编写一个方法public int roll()
,并将计算1-6之间随机数的逻辑放在那里。您只是输出firstRoll和secondRoll的结果,而不是在方法中再次计算
例如:
public class Dice() {
public int roll() {
return 1 + (int) (Math.random() * 6);
}
}
并使用它:
Dice d1 = new Dice();
Dice d2 = new Dice();
int d1Result = d1.roll();
int d1Result = d2.roll();
while (d1Result != d2Result) {
d1Result = d1.roll();
d2Result = d2.roll();
System.out.println("Result d1: " + d1Result);
System.out.println("Result d2: " + d2Result);
// Rest of logic ...
}
如果骰子不相等(在while循环中),您需要“重新掷骰子”。当前,您只需将它们无限地相加,而不更改两个“角色”的值put firstRoll=1+(int)(Math.random()*6);secondRoll=1+(int)(Math.random()*6);正如你在书中的最后一句话,很明显,你两次得到同一对情侣的概率是1/36,当你滚动3对情侣时,这个概率在天文上是无穷小的。您应该打开调试器以查看控制流。这称为调试。通过自己学习调试,您将几乎像一个魔术师,能够自己回答问题。要求在这么短的时间内调试程序是一种应受谴责的做法。此外,经验法则是消除代码中的重复项(GoogleDry原则)。您可以将公共代码段分解出来(我看到println(“Roll 1,2”)重复两次)。如果骰子不相等(在while循环中),则需要“重新滚动”骰子。当前,您只需将它们无限相加,而不更改两个“角色”的值put firstRoll=1+(int)(Math.random()*6);secondRoll=1+(int)(Math.random()*6);正如你在书中的最后一句话,很明显,你两次得到同一对情侣的概率是1/36,当你滚动3对情侣时,这个概率在天文上是无穷小的。您应该打开调试器以查看控制流。这称为调试。通过自己学习调试,您将几乎像一个魔术师,能够自己回答问题。要求在这么短的时间内调试程序是一种应受谴责的做法。此外,经验法则是消除代码中的重复项(GoogleDry原则)。您可以将公共代码段计算出来(我看到println(“Roll 1,2”)重复两次);但是do/while
循环可以避免重复firstRoll
和secondRoll
的赋值。当然,这是正确的,只是稍微改变了语句的打印方式。最好有一个滚动的方法。我只是想在不改变太多代码的情况下提供一个关于缺少什么的明确答案:-)是的;但是do/while
循环可以避免重复firstRoll
和secondRoll
的赋值。当然,这是正确的,只是稍微改变了语句的打印方式。最好有一个滚动的方法。我只是想在不改变太多代码的情况下,提供一个关于缺失内容的明确答案:-)太好了。我还没有学会创建和使用方法:P顺便说一句,有什么好的在线资源可以用来练习Java吗?我建议阅读Oracle指南和教程:-)特别是面向对象的范例(方法、类等)非常好。我还没有学会创建和使用方法:P顺便说一句,有什么好的在线资源来练习Java吗?我建议阅读Oracle指南和教程:-)特别是面向对象的范例(方法、类等)