Ok so it is my first semester in Computer Science in College and we have this assignment which is to write a "Guessing Game". Basically the computer generates a random number from 1 to 100 and you have 10 tries to guess it right. Each time you get it wrong, it tells you whether you're over it or under it, giving you hints for you to guess. Then you have the option of typing in Y or N to play again. This is where I am having a problem. As odd as it is, yesterday it was working fine and nothing was modified I swear! It's as the language has been changed a little or something. This is the end part of it.. What is in red is where the problem is...
See I want the user to be able to input Y or y to play again. However, as soon as I add the or sign || it doesn't work anymore. Although the answer is Y or y, it leads me to the part where it says that if the answer is not equal to Y or y or N or n, print Your answer is inappropriate... Now is I take out all the || and just leave 'Y' for instead, although the Y answer would have to be capital letter only, the program works... I hope this is clear enough... Anyway I am having the same problem with another program I'm writing... and i swear it worked the way it is written below yesterday...
do
{
System.out.println("Do you want to play again Y/N?\n");
String character = kb.next();
answer = character.charAt(0);
System.out.println();
if(answer == 'N' || answer == 'n')
{
System.out.println("The program will now terminate...");
System.exit(0);
}
if(answer != 'Y' || answer != 'y'|| answer != 'N' || answer != 'n')
{
System.out.println("Your answer is inappropriate...\n");
}
} while(answer != 'Y'|| answer != 'y' || answer != 'N' || answer != 'n');
System.out.println("Guess the number...\n");
} while(answer == 'Y' || answer == 'y');
See I want the user to be able to input Y or y to play again. However, as soon as I add the or sign || it doesn't work anymore. Although the answer is Y or y, it leads me to the part where it says that if the answer is not equal to Y or y or N or n, print Your answer is inappropriate... Now is I take out all the || and just leave 'Y' for instead, although the Y answer would have to be capital letter only, the program works... I hope this is clear enough... Anyway I am having the same problem with another program I'm writing... and i swear it worked the way it is written below yesterday...
do
{
System.out.println("Do you want to play again Y/N?\n");
String character = kb.next();
answer = character.charAt(0);
System.out.println();
if(answer == 'N' || answer == 'n')
{
System.out.println("The program will now terminate...");
System.exit(0);
}
if(answer != 'Y' || answer != 'y'|| answer != 'N' || answer != 'n')
{
System.out.println("Your answer is inappropriate...\n");
}
} while(answer != 'Y'|| answer != 'y' || answer != 'N' || answer != 'n');
System.out.println("Guess the number...\n");
} while(answer == 'Y' || answer == 'y');
Comment