【问题标题】:Infinite while loop inside of while loop javawhile循环java中的无限while循环
【发布时间】:2014-03-29 00:48:38
【问题描述】:

所以我在模拟掷骰子游戏时遇到了问题。除了 while 循环中的 while 循环外,一切都正常运行。调试时,sum 变量保持它的值,newSum 变量在每次迭代中都在变化,并且经常达到 7 和sum 变量的值。如果我注释掉嵌套的 while 循环,并将其设置为 wins++;,那么代码将正确执行,达到预期值。所以我很确定问题出在嵌套循环内。

感谢您的所有意见!

import java.util.Random;
import java.text.DecimalFormat;

public class Ch3Ex2
{
        public static void main (String[] args)
    {
        Random rng = new Random();
        int counter = 0;
        int sum = 0;
        int wins = 0;
        int losses = 0;
        int newSum = 0;
        int reroll1 = 0;
        int reroll2 = 0;
        while (counter < 10000)
        {
                int die1 = rng.nextInt(6) + 1;
                int die2 = rng.nextInt(6) + 1;
            sum = die1 + die2;

            if ((sum == 7) || (sum == 11))
                wins++; 

            else if ((sum == 2) || (sum == 3) || (sum == 12))
                losses++;

            else
            {
                while((newSum != sum) || (newSum != 7))
                {                   
                    reroll1 = rng.nextInt(6) + 1;
                    reroll2 = rng.nextInt(6) + 1;
                    newSum = reroll1 + reroll2;
                }
                if (newSum == sum)
                {
                    wins++;
                }
                else
                {
                        losses++;
                }

            }
            counter++;
        }
        DecimalFormat percent = new DecimalFormat("0.00%");
        double winDenom = wins + losses;
        double winRate = wins/winDenom;
        System.out.print("Your chance of winning a game of craps is : ");
        System.out.println(percent.format(winRate));
    }

}

【问题讨论】:

  • 除非sum 等于7,否则这些条件中的至少一个总是true
  • 蹩脚的代码格式和不连贯的括号样式的问题在于,人们会遇到许多其他情况下根本不会想到的错误。
  • 你能详细说明 Smutje 有什么问题吗?这就是我被教导的方式,除了两次缩进“losses++:”一次..
  • 大多数人转向总是将 { } 放在 if/etc 中的语句周围是有原因的,即使对于单个语句也是如此。

标签: java while-loop nested-loops


【解决方案1】:

无限循环就在这个blook中:

while((newSum != sum) || (newSum != 7))
{                   
    reroll1 = rng.nextInt(6) + 1;
    reroll2 = rng.nextInt(6) + 1;
    newSum = reroll1 + reroll2;
}

因为如果你在第一次启动时得到了一个 not 7 值,它将永远为真并且不会停止。

我认为您应该将 || 替换为 &amp;&amp;

所以它可能看起来像这样:

while((newSum != sum) && (newSum != 7))
{                   
    reroll1 = rng.nextInt(6) + 1;
    reroll2 = rng.nextInt(6) + 1;
    newSum = reroll1 + reroll2;
}

【讨论】:

    【解决方案2】:
               while((newSum != sum) || (newSum != 7))
    

    这个逻辑是不正确的。目前它只有在 sum 等于 7 时才会退出。

    你需要使用 && 而不是 ||。

    【讨论】:

      【解决方案3】:

      在您的算法(newSum == sum)(newSum == 7) 条件下获胜,因此您将使用与此相反的操作。逻辑否定后

      ¬(x 或 y) = ¬x 和 ¬y

      您将获得此解决方案。这意味着您需要将 while 条件更改为 (newSum != sum) &amp;&amp; (newSum != 7)

      【讨论】:

        【解决方案4】:

        您的程序仍然会出错,因为您从未在嵌套的 while 循环中更新 newSum。

        一旦您进入嵌套的 while 循环,您的 newSum 就会设置为 7。然后它就不会再改变了。那么这将使您的获胜机会达到 20% 左右。

        因此,您需要在嵌套的 while 循环终止后将 newSum 更新为 0。那么获胜的变化将是大约 50%。你应该

                else
                {
                    while((newSum != sum) || (newSum != 7))
                    {                   
                        reroll1 = rng.nextInt(6) + 1;
                        reroll2 = rng.nextInt(6) + 1;
                        newSum = reroll1 + reroll2;
                    }
                    if (newSum == sum)
                    {
                        wins++;
                    }
                    else
                    {
                            losses++;
                    }
                    newSum = 0;
        
                }
        

        【讨论】:

          猜你喜欢
          • 2015-06-25
          • 2012-12-24
          • 2013-02-22
          • 2011-11-19
          • 1970-01-01
          • 1970-01-01
          • 2021-02-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多