简体   繁体   English

在while循环java中的无限while循环

[英]Infinite while loop inside of while loop java

So I'm having an issue simulating a game of craps. 所以我在模拟掷骰子游戏时遇到问题。 Everything runs properly except for the while loop within the while loop. 除了while循环中的while循环外,其他所有程序都可以正常运行。 When debugging, the sum variable is retaining it's value, the newSum variable is changing in every iteration, and often hitting 7 and the sum variable's value. 调试时, sum变量将保留其值, newSum变量在每次迭代中都会更改,并且经常会达到7和sum变量的值。 If I comment out the nested while loop, and just have it as wins++; 如果我注释掉嵌套的while循环,并将其作为wins++; , then the code executes properly, to an expected value. ,然后代码将正确执行,并达到期望值。 So I'm quite certain the issue is within the nested loop. 因此,我可以肯定问题出在嵌套循环内。

Thanks for all your input!! 感谢您的输入!!

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

}

The infinite loop is in this blook: 无限循环在此blook中:

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

because if you got a not 7 value in the first start, it will always be true and not stop. 因为如果您在初次启动时得到的值不是7 ,那么它将始终为true,并且不会停止。

i think you should replace the || 我认为您应该替换|| with && &&

so it could look like this: 所以它看起来像这样:

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

This logic is incorrect. 此逻辑不正确。 At the moment it will only ever exit if sum is equal to 7. 目前,只有在sum等于7时才会退出。

You need to use && not ||. 您需要使用&&而不是||。

At your algorithm (newSum == sum) or (newSum == 7) conditions win so you will use the opposite of this action. 在您的算法(newSum == sum)(newSum == 7)条件下获胜,因此您将使用与此操作相反的操作。 After the logical negation 逻辑否定后

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

you will have this solution. 您将拥有此解决方案。 That means you need to change your while condition as (newSum != sum) && (newSum != 7) . 这意味着您需要将while条件更改为(newSum != sum) && (newSum != 7)

Your program will still be wrong as you never update newSum in nested while loop. 您的程序仍然会出错,因为您永远不会在嵌套的while循环中更新newSum。

Once you enter the nested while loop, your newSum set to 7. Then it won't change anymore. 一旦进入嵌套的while循环,您的newSum将设置为7。然后它将不再更改。 Then this would cause your win chance to about 20%. 然后,这将使您的获胜几率提高到20%。

So you need update the newSum to 0 after a nested while loop terminate. 因此,您需要在嵌套的while循环终止后将newSum更新为0。 Then the win change would be about 50%. 那么获胜的变化将是大约50%。 You should 你应该

        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;

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM