I'm having a problem with an infinite loop and not 100% sure where the issue is.
I'm unsure if there is an issue within my "while" statement or there's a wrong sign or bracket.
This is my first time doing this and seems it doesn't like my post being "mostly code", so disregard this paragraph of me typing away to remove the message :)
import java.util.Random; // Needed for the Random class
import java.util.Scanner; // Needed for the Scanner class
/**
This class simulates rolling a pair of dice 10,000 times
and counts the number of times doubles of are rolled for
each different pair of doubles.
*/
public class RollDice {
public static void main(String[] args){
final int NUMBER = 10000; // Number of dice rolls
// A random number generator used in
// simulating the rolling of dice
Random generator = new Random();
int die1Value; // Value of the first die
int die2Value; // Value of the second die
int count = -1; // Total number of dice rolls
int snakeEyes = 0; // Number of snake eyes rolls
int twos = 0; // Number of double two rolls
int threes = 0; // Number of double three rolls
int fours = 0; // Number of double four rolls
int fives = 0; // Number of double five rolls
int sixes = 0; // Number of double six rolls
int runAgain = 0; // Sentinel variable
Scanner keyboard = new Scanner(System.in); //Scanner Object
while(runAgain == 0){ //This is a sentinel loop
// TASK #1 Enter your code for the algorithm here
while(count < NUMBER)
{
// Roll both dice
die1Value = generator.nextInt(6) 1;
die2Value = generator.nextInt(6) 1;
// Check to see if both dice are equal
if(die1Value == die2Value)
{
// Check dice values and update counts
if(die1Value == 1)
snakeEyes ;
else if(die1Value == 2)
twos ;
else if(die1Value == 3)
threes ;
else if(die1Value == 4)
fours ;
else if(die1Value == 5)
fives ;
else
sixes ;
}
count ;
}
// Display the results
System.out.println ("You rolled snake eyes "
snakeEyes " out of "
count " rolls.");
System.out.println ("You rolled double twos "
twos " out of " count
" rolls.");
System.out.println ("You rolled double threes "
threes " out of " count
" rolls.");
System.out.println ("You rolled double fours "
fours " out of " count
" rolls.");
System.out.println ("You rolled double fives "
fives " out of " count
" rolls.");
System.out.println ("You rolled double sixes "
sixes " out of " count
" rolls.");
}
// Reset all counts [Every time you want to run again]
snakeEyes = 0;
twos = 0;
threes = 0;
fours = 0;
fives = 0;
sixes = 0;
count = 0;
// Ask the user if they wish to run again, 0 = yes, -1 = no
System.out.println("Do you wish to run again?");
System.out.println("Enter a 0 for yes or a -1 for no");
// Read user's response
runAgain = keyboard.nextInt();
}
}
CodePudding user response:
I formatted your code and in addition to what Brakke Baviaan Says, this part of your code should be inside the while loop:
// Reset all counts [Every time you want to run again]
snakeEyes = 0;
twos = 0;
threes = 0;
fours = 0;
fives = 0;
sixes = 0;
count = 0;
// Ask the user if they wish to run again, 0 = yes, -1 = no
System.out.println("Do you wish to run again?");
System.out.println("Enter a 0 for yes or a -1 for no");
// Read user's response
runAgain = keyboard.nextInt();
Currently is outside so thats why it keeps looping as you never get to update the variable runAgain
CodePudding user response:
You are using a sentinel variable. A sentinel variable is a variable that controls the termination of a loop. In your case, as the comment points out, the sentinel variable runAgain never gets updated, thus the loop will continue infinately.
Boolean sentinel = true;
while(sentinel){
doSomething();
if(someCondition == true){
sentinel = false; //loop will end after function reaching this point
};
CodePudding user response:
The count reset should be inside the first while.