So I've been using java for a few weeks now and wanted to try to make my first little game. Everything I had was working fine until I tried adding a play again feature. It looks like it just skipping a small portion of code at the bottom and I can't figure out why.
I've tried moving different parts of the code around but can't figure out why it's not working.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("You have 10 tries to correctly guess a random number between 1 and 10. Good luck! \n"
+ "After you have entered a number, press ENTER to see if you won.");
boolean play;
play = true;
do
{
int tries = 10, triesLeft = 0, triesUsed = 0;
String YorN;
do
{
System.out.print("\nEnter A Number: ");
int numInp = scan.nextInt();
int randomNumber = (int)(Math.random()*9) + 1;
System.out.println("The number was " + randomNumber);
if(numInp == randomNumber) {
System.out.println("\nYou Win! Great job.");
triesLeft = --tries;
triesUsed = 10 - triesLeft;
tries = -1;
} else {
System.out.println("Try Again...");
tries --;
}
}
while( tries > 0);
String playAgain = (" Feel free to play again soon!");
if(tries == 0) {
System.out.println("\nOut of tries :( You lose loser " + playAgain);
}
if(tries == -1) {
System.out.println("\nYou used " + triesUsed + " tries and had "
+ triesLeft + " tries left! :)" + playAgain);
}
System.out.print("\nDo you want to play again? Y or N?");
YorN = scan.nextLine();
if (YorN == ("Y") || YorN == ("y"))
{
play = true;
}
else if (YorN == ("N") || YorN == ("n"))
{
play = false;
}
else {}
} while(play = true);
if (play = false){
System.out.println("\nGood Bye");
}
}
This system doesn't seem to be doing anything when I run the code:
YorN = scan.nextLine();
if (YorN == ("Y") || YorN == ("y"))
{
play = true;
}
else if (YorN == ("N") || YorN == ("n"))
{
play = false;
}
else {}
Here's what I see when I run it:
You Win! Great job.
You used 5 tries and had 5 tries left! :) Feel free to play again soon!
Do you want to play again? Y or N? Enter A Number:
It doesn't give me a spot to type y or n as it should. If anyone knows why I would really appreciate the help