Computer Science Canada

Or not equal too question.

Author:  thegadgetman [ Fri Nov 26, 2010 11:14 am ]
Post subject:  Or not equal too question.

I am having an issue with my While statement when asking user if they want to loop the program or not. Even if I put the correct response "Y" or "N" it still loops through the error check, I am assuming my logical OR statement is the cause but I am not sure how to fix it.

code:

//Ask user if they want to play again
System.out.print("\n\t\t Would you like to play again?(Y/N) ");
playAgain = userInput.next();
playAgain = playAgain.toUpperCase();
           //Error check  to confirm that the users response was valid
           while(!playAgain.equals("Y") || !playAgain.equals("N"))
                 {
                     System.out.println("ERROR: Your entry " + playAgain + " is not valid please enter Y for yes or N for no.");
                     System.out.print("\n\t\t Would you like to play again?(Y/N) ");
                     playAgain = userInput.next()
                     playAgain = playAgain.toUpperCase();
                 }

Author:  jcollins1991 [ Fri Nov 26, 2010 12:17 pm ]
Post subject:  Re: Or not equal too question.

Because you're looping both when it's neither of the two (or statements evaluates !F | !F, so T | T), and when one of them is true (or statement evaluates !T | !F, so T | F). What you want is either (!A & !B) or (!(A | B)).

Author:  thegadgetman [ Sat Nov 27, 2010 12:33 pm ]
Post subject:  Re: Or not equal too question.

Okay I see why now. Thank you for taking the time to explain it to me and not just give me the answer.


: