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!