【问题标题】:Dice Doubles Java Exercise - Logical Error骰子加倍 Java 练习 - 逻辑错误
【发布时间】:2016-03-07 16:37:18
【问题描述】:

我是一名初学者,通过在线资源自学 Java。我遇到过这个练习。

编写一个程序,通过从 1-6 中选择一个随机数,然后从 1-6 中选择第二个随机数来模拟掷骰子。将两个值相加,并显示总数。修改你的骰子游戏,让它一直滚动,直到它们得到双倍(两个骰子上的数字相同)。

到目前为止我有这个代码:

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 = 1 + (int) (Math.random() * 6); secondRoll = 1 + (int) (Math.random() * 6);作为你在 while 循环中的最后一条语句
  • 很明显,你两次得到同一对的概率是 1/36,当你掷出 3 对时,这个概率是天文数字的无穷小。您应该打开一个调试器来查看控制流。这称为debugging。自己学习调试,你就像魔术师一样,自己回答问题。要求在 SO 调试您的程序是一种应受谴责的做法。此外,经验法则是消除代码中的重复项(google DRY 原则)。您将常见的代码段排除在外(我看到 println("Roll 1,2") 重复了两次)。

标签: java


【解决方案1】:

如果它们的值不同,您必须再次掷骰子。

在这种情况下使用do-while 很好。

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);

【讨论】:

    【解决方案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);
    }
    

    【讨论】:

    • 是的;但是do/while 循环将避免重复分配firstRollsecondRoll 的需要。
    • 当然是对的,只是稍微改变了报表的打印方式。最好有一个滚动的方法。我只是想在不更改太多代码的情况下就缺少的内容提供一个明确的答案:-)
    【解决方案3】:

    编写一个方法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 ... 
    
    }
    

    【讨论】:

    • 很棒的方法。我还没有学会创建和使用方法 :P 有什么好的在线资源可以顺便练习 Java 吗??
    • 我建议阅读 Oracle 指南和教程 :-) docs.oracle.com/javase/tutorial/java/concepts 特别是面向对象的范式(方法、类等)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多