# Why is this in an infinite loop?

• November 12th, 2013, 02:17 PM
Ludus
Why is this in an infinite loop?
Im new to this site so the format of this post may be sub par. I have been working on this code and I cannot seem to figure out why when option one is chosen it asks for the number and does nothing. Thank you for any feedback you may be able to assist me with.

import java.util.*;

public class Lab12
{

// Code for implementing option 1 of lab assignment
// Roll three six-sided dice until they all show a different number
// Print out the result
public static void option1()
{
//System.out.println("Executing option 1");
// Code goes here
Dice die1 = new Dice();
Dice die2 = new Dice();
Dice die3 = new Dice();
boolean finished = false;
int getNumRolls;
int count = 0;
int r1 = die1.roll();
int r2 = die2.roll();
int r3 = die3.roll();
while (!finished)
{
//int r1 = die1.roll();
//int r2 = die2.roll();
//int r3 = die3.roll();
if (r1 != r2) {
r1 = die1.roll();
r2 = die2.roll();
r3 = die3.roll();
}
else if (r2 != r3) {
r1 = die1.roll();
r2 = die2.roll();
r3 = die3.roll();
}
else if (r3 != r1) {
finished = true;
//System.out.println("It took " + count + " rolls to roll three different numbers: " + r1 + ", " + r2 +", " + r3 + ".");
}
}
getNumRolls = die1.getNumRolls();
System.out.println("It took " + getNumRolls + " rolls to roll three different numbers: " + r1 + ", " + r2 +", " + r3 + ".");
}

// Code for implementing option 2 of lab assignment
// Roll the two dice n times and print the average.
public static void option2(int n)
{
//System.out.println("Executing option 2");
Dice die1 = new Dice();
Dice die2 = new Dice();
int r1 = 0;
int r2 = 0;
for (int i = 1; i <= n; i++) {
r1 = r1 + die1.roll();
r2 = r2 + die2.roll();
}
int avg = (r2+r1)/n;
System.out.println("The average of " + n + " roll(s) of the two dice is " + avg);
// Code goes here

}

// Must add code to main for system prompts and Case 3

public static void main(String [] args)
{
• November 12th, 2013, 02:23 PM
Norm
Re: Why is this in an infinite loop?
[code=java]
[/code]
to get highlighting and preserve formatting.

The posted code looks like the end of the code is missing.
• November 12th, 2013, 02:29 PM
Ludus
Re: Why is this in an infinite loop?
I believe that's how you asked.

There is the whole code
Code java:

```    import java.util.*;   public class Lab12 {   // Code for implementing option 1 of lab assignment // Roll three six-sided dice until they all show a different number // Print out the result public static void option1() { //System.out.println("Executing option 1"); // Code goes here Dice die1 = new Dice(); Dice die2 = new Dice(); Dice die3 = new Dice(); boolean finished = false; int getNumRolls; int count = 0; int r1 = die1.roll(); int r2 = die2.roll(); int r3 = die3.roll(); while (!finished) { if (r1 == r2) { r1 = die1.roll(); r2 = die2.roll(); r3 = die3.roll(); } else if (r2 == r3) { r1 = die1.roll(); r2 = die2.roll(); r3 = die3.roll(); } else if (r3 == r1) { finished = true; //System.out.println("It took " + count + " rolls to roll three different numbers: " + r1 + ", " + r2 +", " + r3 + "."); } } getNumRolls = die1.getNumRolls(); //System.out.println("It took " + getNumRolls + " rolls to roll three different numbers: " + r1 + ", " + r2 +", " + r3 + "."); }       // Code for implementing option 2 of lab assignment // Roll the two dice n times and print the average. public static void option2(int n) { //System.out.println("Executing option 2"); Dice die1 = new Dice(); Dice die2 = new Dice(); int r1 = 0; int r2 = 0; for (int i = 1; i <= n; i++) { r1 = r1 + die1.roll(); r2 = r2 + die2.roll(); } int avg = (r2+r1)/n; System.out.println("The average of " + n + " roll(s) of the two dice is " + avg); // Code goes here   }   // Must add code to main for system prompts and Case 3   public static void main(String [] args) { Scanner keyboard = new Scanner(System.in);   boolean finished = false; // loop termination flag System.out.println("Welcome to the Dice Rolling Sample Program"); //System.out.println("Enter choice (1-count rolls; 2-average roll; 3-terminate program"); //int choice = keyboard.nextInt(); while(!finished) { System.out.println("Enter choice (1-count rolls; 2-average roll; 3-terminate program"); int choice = keyboard.nextInt();   if (choice == 1) { option1(); System.out.println("It took " + getNumRolls + " rolls to roll three different numbers: " + r1 + ", " + r2 +", " + r3 + "."); } else if (choice == 2) { System.out.println("Enter number of rolls"); int numRolls = keyboard.nextInt(); option2(numRolls); } else if (choice == 3) { // code for choice = 3 goes here finished = true; System.out.println("Thank you for using the progam. Good-bye"); } } } }```
• November 12th, 2013, 02:35 PM
Norm
Re: Why is this in an infinite loop?
Hard to test if the code does not compile.
• November 12th, 2013, 02:37 PM
Ludus
Re: Why is this in an infinite loop?
Thought I posted the compiled version. I will fix and edit it.
• November 12th, 2013, 02:59 PM
Norm
Re: Why is this in an infinite loop?
The posted code is missing the Dice class
• November 12th, 2013, 09:32 PM
Junky
Re: Why is this in an infinite loop?
Quote:

Originally Posted by Ludus
Thought I posted the compiled version.

You are trying to access variables out of scope all over the place. I highly doubt this code compiles.
• November 19th, 2013, 04:18 AM
arnavkumartechno
Re: Why is this in an infinite loop?
It is not the complete program.
• November 19th, 2013, 05:40 AM
Norm
Re: Why is this in an infinite loop?