简体   繁体   中英

Why is this if-statement triggering in this While-loop?

I'm new to Java, and I am creating a while-loop with one of the conditions being:

if ((userChoice != 'p') || (userChoice != 'P') || (userChoice != 's') || (userChoice != 'S')) System.out.println("*** Use P or S, please. ***");

Why is it that when I enter "p","P", "s", or "S" the program still outputs "" * Use P or S, please. * "??

This is the whole program:

import java.util.Scanner;

public class Foothill
{
   public static void main(String[] args)
   {
      // declare an object that can be used for console input
      Scanner inputStream = new Scanner(System.in);

      // declare variables
      String strUserInput;
      char userChoice, userCredits;
      int numYogurts, yogurtWallet = 0;
      // while loop for full transaction
      while (true)
      {
      // menu message
          System.out.println("Menu: \n P (process Purchase) \n S (Shut down)");
          strUserInput = inputStream.nextLine();
          userChoice = strUserInput.charAt(0);

        // condition that forces users to select only P or S
          if ((userChoice != 'p') || (userChoice != 'P') || (userChoice != 's') || (userChoice != 'S'))
              System.out.println("*** Use P or S, please. ***");

          System.out.println("Your choice:" + userChoice);
        // if condition that starts purchase part of transaction
          if ( (userChoice == 'p') || (userChoice == 'P') ) 
          {
              System.out.println("How many yogurts would you like to buy?");
              strUserInput = inputStream.nextLine();
              numYogurts = Integer.parseInt(strUserInput);

              yogurtWallet += numYogurts;

              System.out.println("You just earned " + numYogurts + " stamps and have a total of " + yogurtWallet + " to use");
              // if condition that tests number of purchased yogurts
              if (yogurtWallet >= 10)
              {
                  System.out.println("You qualify for a free yogurt. Would you like to use your credits? (Y or N)");
                  strUserInput = inputStream.nextLine();
                  userCredits = strUserInput.charAt(0);

                  if ((userCredits == 'Y') || (userCredits == 'y'))
                  {
                      yogurtWallet -= 10;
                      System.out.println("You have just used 10 credits and have " + yogurtWallet + " left. Enjoy your free yogurt.");
                  }
              }
          } 
          // if condition that stops the program 
          if ( (userChoice == 's') || (userChoice == 'S') )
          {
              System.out.println("Goodbye!");
              break;
          }

      }
   }
}

Assume you entered p . Due to Short Circuit Evaluation , Java will continue and check all the conditions inside the if statement, the next check is != 'P' , which is true ! The same holds for the other inputs:

userChoice |  != 'p' | != 'P' | != 's' | != 'S'
-----------+---------+--------+--------+-------
    p      |   Yes   |   --   |   --   |  --
    P      |   No    |   Yes  |   --   |  --
    s      |   No    |   No   |   Yes  |  --
    S      |   No    |   No   |   No   |  Yes

-- means not evaluated because of Short Circuit Evaluation .

So in all cases, your if is satisfied!

No matter which letter you choose, it will still be not equal to the other choices. || means OR. So in A OR B, if A is true, or B is true, then the entire condition is true. What you need to do is use &&, for AND.

use if ((userChoice != 'p') && (userChoice != 'P') && (userChoice != 's') && (userChoice != 'S'))

This will solve your issue.

if ((userChoice != 'p') && (userChoice != 'P') && 
   userChoice != 's') && (userChoice != 'S')) 
       System.out.println("*** Use P or S, please. ***");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM