Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Why is this in an infinite loop?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    Greenville NC
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)
    {


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is this in an infinite loop?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    The posted code looks like the end of the code is missing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Location
    Greenville NC
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is this in an infinite loop?

    I believe that's how you asked.


    There is the whole code
     
     
    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");     
           }
         }
      }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is this in an infinite loop?

    Hard to test if the code does not compile.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Location
    Greenville NC
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is this in an infinite loop?

    Thought I posted the compiled version. I will fix and edit it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is this in an infinite loop?

    The posted code is missing the Dice class
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Why is this in an infinite loop?

    Quote Originally Posted by Ludus View Post
    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.
    Improving the world one idiot at a time!

  8. #8
    Junior Member
    Join Date
    Oct 2013
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Why is this in an infinite loop?

    It is not the complete program.
    Last edited by copeg; November 19th, 2013 at 03:19 PM. Reason: Removed unrelated link

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is this in an infinite loop?

    Please post the code here on the forum, not a link.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why do I get an infinite loop?
    By Skynet928 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 22nd, 2013, 08:29 PM
  2. Infinite loop?
    By jackfletcher in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2012, 01:06 PM
  3. Infinite loop problem
    By jacobjc3 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 30th, 2012, 09:41 PM
  4. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM