简体   繁体   中英

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. 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. If I comment out the nested while loop, and just have it as 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:

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.

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.

You need to use && not ||.

At your algorithm (newSum == sum) or (newSum == 7) conditions win so you will use the opposite of this action. After the logical negation

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

you will have this solution. That means you need to change your while condition as (newSum != sum) && (newSum != 7) .

Your program will still be wrong as you never update newSum in nested while loop.

Once you enter the nested while loop, your newSum set to 7. Then it won't change anymore. Then this would cause your win chance to about 20%.

So you need update the newSum to 0 after a nested while loop terminate. Then the win change would be about 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;

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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