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

Java 为什么这个if语句会在While循环中触发?,java,Java,我是Java新手,我正在创建一个while循环,其中一个条件是: if((userChoice!=“p”)|(userChoice!=“p”)|(userChoice!=“s”)|(userChoice!=“s”)) System.out.println(“***请使用P或S.***”) 为什么当我输入“p”、“p”、“s”或“s”时,程序仍然输出“*请使用p或s”*“?” 这是整个计划: import java.util.Scanner; public class Foothill {

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

if((userChoice!=“p”)|(userChoice!=“p”)|(userChoice!=“s”)|(userChoice!=“s”))
System.out.println(“***请使用P或S.***”)

为什么当我输入“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;
          }

      }
   }
}

无论你选择哪个字母,它仍然不等于其他选择。| |表示或。因此,在A或B中,如果A为真,或B为真,则整个条件为真。你需要做的是使用&,for AND。

无论你选择哪个字母,它仍然不等于其他选择。| |表示或。因此,在A或B中,如果A是true或B为真,则整个条件为真。您需要做的是使用&&,for和。

使用
if((userChoice!=“p”)&&&(userChoice!=“s”)&&(userChoice!=“s”)&(userChoice!=“s”)

这将解决您的问题。

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

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

这将解决您的问题。

假设您输入了
p
。因此,Java将继续检查
if
语句中的所有条件,下一个检查是
!=“p”
,这是
true
!对于其他输入也同样适用:

if ((userChoice != 'p') && (userChoice != 'P') && 
   userChoice != 's') && (userChoice != 'S')) 
       System.out.println("*** Use P or S, please. ***");
userChoice |  != 'p' | != 'P' | != 's' | != 'S'
-----------+---------+--------+--------+-------
    p      |   Yes   |   --   |   --   |  --
    P      |   No    |   Yes  |   --   |  --
    s      |   No    |   No   |   Yes  |  --
    S      |   No    |   No   |   No   |  Yes
--
表示由于以下原因而未进行评估


因此,在所有情况下,您的
if
都是满意的!

假设您输入了
p
。因此,Java将继续检查
if
语句中的所有条件,下一个检查是
!=“p”
,这是
true
!对于其他输入也同样适用:

userChoice |  != 'p' | != 'P' | != 's' | != 'S'
-----------+---------+--------+--------+-------
    p      |   Yes   |   --   |   --   |  --
    P      |   No    |   Yes  |   --   |  --
    s      |   No    |   No   |   Yes  |  --
    S      |   No    |   No   |   No   |  Yes
--
表示由于以下原因而未进行评估


因此,在所有情况下,如果您的
满意!

您的
|
能做什么?将| |更改为&&它会工作
|
能做什么?将| |更改为&&它会工作热电路条件是OPs问题和bug占位符。@nikpon是的。这是我澄清的。它不会检查所有条件,它会在第一个原因是
true
。放炮电路条件是OPs问题和bug的占位符。@nikpon是的。这就是我澄清的。它不会检查所有条件,它会在第一个原因是
true
。A=B不等于B=A逻辑不是Java,但Java有逻辑操作。A=B不等于B=A逻辑不是Aava但是Java有逻辑运算。你能不能让它成为一种实践来接受你使用并有效的答案,它可能也会帮助其他人。:)你能让它成为一种实践来接受你使用并有效的答案,它可能也会帮助其他人。:)