Hi All,
For homework I had to write a "guessing game" program, which I decided to take to another level and add on the option to add names and keep a score, etc. The problem is however, That my program will not display the text "Player Has Won! Congratulations!".
Here is my code:

public class prog210b
{
     public static void main (String[] args)
        {
         //title
         System.out.println("The Guessing Game!");
         System.out.println("------------------");
         System.out.println("The Person to guess in less tries wins a Point! First to 5 Wins!\n");
         //Player System
         System.out.print("Enter the name of Player 1: ");
         Scanner p1 = new Scanner(System.in);
         String player1 = p1.next();
         System.out.print("Enter the name of Player 2: ");
         Scanner p2 = new Scanner(System.in);
         String player2 = p2.next();
         int points1=0;
         int points2=0;
         //Game Start
         int count = 0;
         int roundct=0;
         int p1count=0;
         int p2count=0;
         //Processing
         while(points1<6 || points2<6)
            {

                for(int r=1;r<=11;r++)
                {

                    if(r%2==0)
                    {
                        System.out.println("--------------------");
                        System.out.println("\n" +player2 +"'s Turn");
                        System.out.println("Im thinking of a number between 1 and 100\n");
                        Random Rnumber = new Random();
                        int Anumber = Rnumber.nextInt(1);//REMEMBER TO CHANGE BACK to 101
                        System.out.print(player2 +" What do you think it is?: ");
                        Scanner n = new Scanner(System.in);
                        int number = n.nextInt();
                        while(number!=Anumber)
                        {

                            if(number>Anumber)
                            {System.out.print("Lower--Try Again\n");}
                            else if(number<Anumber)
                            {System.out.print("Higher--Try Again\n");}
                            else break;
                            System.out.println("\nWhat do you think it is?:");
                            number = n.nextInt();
                            count+=1;
                        }
                        p2count=count;
                        System.out.println("You got it right in "+(count+1) +" guesses");
                        count=0;
                        roundct+=1;
                    }   
                    else
                    {
                        System.out.println("--------------------");
                        System.out.println("\n" +player1 +"'s Turn");
                        System.out.println("Im thinking of a number between 1 and 100\n");
                        Random R2number = new Random();
                        int Bnumber = R2number.nextInt(1); //REMEMBER TO CHANGE BACK to 101
                        System.out.print(player1 +" What do you think it is?: ");
                        Scanner n2 = new Scanner(System.in);
                        int number2 = n2.nextInt();
                        while(number2!=Bnumber)
                        {

                            if(number2>Bnumber)
                            {System.out.print("Lower--Try Again\n");}
                            else if(number2<Bnumber)
                            {System.out.print("Higher--Try Again\n");}
                            else break;
                            System.out.println("\nWhat do you think it is?:");
                            number2 = n2.nextInt();
                            count+=1;
                        }
                        p1count=count;
                        System.out.println("You got it right in "+(count+1) +" guesses");
                        count=0;
                        roundct+=1;
                    }
                    //roundct+=1;
                    if(points1==5)
                            {System.out.println(player1 +" Has won the game! Congratulations!");
                             System.exit(0);
                            }
                    if(points2==5)
                            {System.out.println(player1 +" Has won the game! Congratulations!");
                             System.exit(0);
                            }
                    if(roundct==2)
                        {

                         System.out.println("--------------------");
                         if(p1count==p2count)
                            {System.out.println("It was a draw. No one Gets a point this round");
                             }
                         if(p1count>p2count)
                            {System.out.println(player2 +" Won this round and gains a point");
                             points2+=1;
                             System.out.println("Score:\n" +player1 +": " +points1 +"\n"  +player2 +": " +points2);
                             p1count=0;
                             p2count=0;
                             }
                         if(p1count<p2count)
                            {System.out.println(player1 +" Won this round and gains a point");
                             points1+=1;
                             System.out.println("Score:\n" +player1 +": " +points1 +"\n"  +player2 +": " +points2);
                             p1count=0;
                             p2count=0;
                             }
                          roundct=0;
                                }


                    }
                    }

                }
            }

BTW i have tried to put the

                    if(points1==5)
                            {System.out.println(player1 +" Has won the game! Congratulations!");
                             System.exit(0);
                            }
                    if(points2==5)
                            {System.out.println(player1 +" Has won the game! Congratulations!");
                             System.exit(0);
                            }

Everywhere, but it wont work.
What am I doing wrong?
Thanks!

Recommended Answers

All 7 Replies

Can you post the console that shows the input to the program and what the program prints out?

In windows To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Heres the output:
BTW its set to choose either 0 or 1 to make checking the program faster

'The Guessing Game!

The Person to guess in less tries wins a Point! First to 5 Wins!

Enter the name of Player 1: Dave

Enter the name of Player 2: John

Dave's Turn
Im thinking of a number between 1 and 100

Dave What do you think it is?: 1
Lower--Try Again

What do you think it is?:
0

You got it right in 2 guesses

John's Turn
Im thinking of a number between 1 and 100

John What do you think it is?: 0

You got it right in 1 guesses

John Won this round and gains a point
Score:
Dave: 0

John: 1

Dave's Turn
Im thinking of a number between 1 and 100

Dave What do you think it is?: 1
Lower--Try Again

What do you think it is?:
0

You got it right in 2 guesses

John's Turn
Im thinking of a number between 1 and 100

John What do you think it is?: 0

You got it right in 1 guesses

John Won this round and gains a point
Score:
Dave: 0

John: 2

Dave's Turn
Im thinking of a number between 1 and 100

Dave What do you think it is?: 1
Lower--Try Again

What do you think it is?:
0

You got it right in 2 guesses

John's Turn
Im thinking of a number between 1 and 100

John What do you think it is?: 0

You got it right in 1 guesses

John Won this round and gains a point
Score:
Dave: 0

John: 3

Dave's Turn
Im thinking of a number between 1 and 100

Dave What do you think it is?: 0

You got it right in 1 guesses

John's Turn
Im thinking of a number between 1 and 100

John What do you think it is?: 1
Lower--Try Again

What do you think it is?:
0

You got it right in 2 guesses

Dave Won this round and gains a point
Score:
Dave: 1

John: 3

Dave's Turn
Im thinking of a number between 1 and 100

Dave What do you think it is?: 0

You got it right in 1 guesses

John's Turn
Im thinking of a number between 1 and 100

John What do you think it is?: 1
Lower--Try Again

What do you think it is?:
0

You got it right in 2 guesses

Dave Won this round and gains a point
Score:
Dave: 2

John: 3

Dave's Turn
Im thinking of a number between 1 and 100

Dave What do you think it is?: 0

You got it right in 1 guesses

Dave's Turn
Im thinking of a number between 1 and 100

Dave What do you think it is?: 1
Lower--Try Again

What do you think it is?:
0

You got it right in 2 guesses

John Won this round and gains a point
Score:
Dave: 2

John: 4

John's Turn
Im thinking of a number between 1 and 100

John What do you think it is?: 0

You got it right in 1 guesses

Dave's Turn
Im thinking of a number between 1 and 100

Dave What do you think it is?: 1
Lower--Try Again

What do you think it is?:
0

You got it right in 2 guesses

John Won this round and gains a point
Score:
Dave: 2

John: 5

John's Turn
Im thinking of a number between 1 and 100

John What do you think it is?: 0
You got it right in 1 guesses`

What would the desired output look like?
Try debugging the code by adding some println statements that print out the values of variables that control program logic especially the printing of the desired message.

The desired output would be that when a player reaches 5 points, the program says "Player Has Won! Congratulations!" and terminates. I tried debugging to no avail :(

What would the correct output look like?

How did you try debugging? Did you print out the value of the players' points to see how it changed and to see why the code did not detect when it reached 5. Also you need to track the roundct variable.

Ok I got it to work. I basically re wrote the ending command so now it looks like this:

if(points1==5)
                                {
                                    System.out.println("--------------------");
                                    System.out.println(player1 +" has reached 5 points total, continue?[Y/N]");
                                    Scanner end = new Scanner(System.in);
                                    String done = end.next();
                                    if(done=="Y")
                                    {System.out.println("The Game will now Continue");
                                        System.out.println("--------------------");}
                                        else
                                        {
                                            System.exit(0);
                                        }    
                                    }
                            if(points2==5)
                                {
                                    System.out.println("--------------------");
                                    System.out.println(player2 +" has reached 5 points total, continue?[Y/N]");
                                    Scanner end = new Scanner(System.in);
                                    String done = end.next();
                                    if(done=="Y")
                                        {System.out.println("The Game will now Continue");
                                            System.out.println("--------------------");}
                                            else
                                            {
                                                System.out.println("Thanks for Playing!");
                                                System.exit(0);
                                            }    
                                        }

Thanks for the help everyone :)

if(done=="Y")

You should the equals() method to compare Strings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.