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 4 of 4

Thread: my code is going in infinite loop

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default my code is going in infinite loop

    public class GuessGame {
    //class GuessGame has three instance variable to hold three player objects
    Player p1;
    Player p2;
    Player p3;

    public void startGame(){

    p1 = new Player(); //assigns new player
    p2 = new Player(); //objects to instance variables
    p3 = new Player(); //of player

    int guessp1 = 0; //holds the guess of p1
    int guessp2 = 0; //holds the guess of p2
    int guessp3 = 0; //holds the guess of p3

    //declare three variables to hold true or false based on player's guess
    boolean p1isRight = false;
    boolean p2isRight = false;
    boolean p3isRight = false;

    int targetNumber = (int) (Math.random() * 10); //generates a random number between 0 and 9
    System.out.println("I m thinking of a number between 0 and 9");

    while(true){
    System.out.println("Number to be guessed is " + targetNumber);

    p1.guess();
    p2.guess();
    p3.guess();
    //get the guesses of all three players through number variable
    guessp1 = p1.number;
    System.out.println("Guess of player p1 is " + guessp1);

    guessp2 = p2.number;
    System.out.println("Guess of player p2 is " + guessp2);

    guessp3 = p3.number;
    System.out.println("Guess of player p3 is " + guessp3);


    if(guessp1 == targetNumber){
    p1isRight = true;
    }

    if(guessp2 == targetNumber){
    p2isRight = true;
    }

    if(guessp3 == targetNumber){
    p3isRight = true;
    }

    if(p1isRight || p2isRight || p3isRight){
    System.out.println("We have a Winner");
    System.out.println("player one got it right? " + p1isRight);
    System.out.println("palyer two got it right? " + p2isRight);
    System.out.println("player three got it right? " + p3isRight);
    System.out.println("game over");

    break;
    }
    else{
    System.out.println("try out another time");
    }
    }
    }

    }


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: my code is going in infinite loop

    Please use code tags when posting code.

    p1.guess();
    p2.guess();
    p3.guess();
    I am thinking guess() is a method defined elsewhere - there is a
    lot of code in the while loop too - but without knowing what happens
    in the guess method, it's hard to pin point the problem. Which part(s)
    of the code are repeating?

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: my code is going in infinite loop

    Please post your code correctly using code or highlight tags which are explained near the top of this link.

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: my code is going in infinite loop

    Do you have any questions or did you just want to show us your code?

Similar Threads

  1. Why is this in an infinite loop?
    By Ludus in forum Loops & Control Statements
    Replies: 8
    Last Post: November 19th, 2013, 06:40 AM
  2. 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
  3. Infinite Loop in my code.
    By Vinster271 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2012, 03:46 AM
  4. Infinite loop?
    By jackfletcher in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2012, 01:06 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