为什么这个if语句在这个While循环中触发?

时间:2013-10-27 16:30:11

标签: java

我是Java的新手,我正在创建一个while循环,其中一个条件是:

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

为什么当我输入“p”,“P”,“s”或“S”时程序仍然输出“” *请使用P或S. * “??

这是整个计划:

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;
          }

      }
   }
}

4 个答案:

答案 0 :(得分:4)

假设您输入了p。由于Short Circuit Evaluation,Java将继续并检查if语句中的所有条件,下一项检查为!= 'P',即true!其他输入也是如此:

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

--表示因Short Circuit Evaluation而无法评估。

所以在所有情况下,您的if都会满意!

答案 1 :(得分:1)

无论您选择哪一个字母,它仍然不等于其他选择。 ||是指OR。所以在A OR B中,如果A为真,或者B为真,则整个条件为真。您需要做的是使用&&,用于AND。

答案 2 :(得分:0)

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

这将解决您的问题。

答案 3 :(得分:0)

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