Java:使用if条件检查char变量的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2256777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:20:02  来源:igfitidea点击:

Java: Checking contents of char variable with if condition

javavariablesconditionalcharif-statement

提问by Troy

I have a char variable that is supposed to contain either a Y,y,n or N character, I want to test if it does not contain it, then display an error message and exit the program.

我有一个应该包含 Y、y、n 或 N 字符的 char 变量,我想测试它是否不包含它,然后显示一条错误消息并退出程序。

This is the code I am using;

这是我正在使用的代码;

    if (userDecision != 'Y' || userDecision != 'y' || userDecision != 'n' || userDecision != 'N')
        {
            System.out.println("Error: invalid input entered for the interstate question");
            System.exit(0);
        }

Irregardless of what is in the variable it always returns true and executes the command to exit the program, what am I doing wrong?

不管变量中有什么,它总是返回true并执行退出程序的命令,我做错了什么?

回答by Chris Jester-Young

You need to use &&instead of ||. You are asking whether "none" of those characters match, not simply whether any one of the four fail to match. (A value cannot simultaneously be Y, y, N, and n.)

您需要使用&&而不是||. 您问的是这些字符中是否“无”匹配,而不仅仅是四个字符中的任何一个是否匹配。(一个值不能同时是 Y、y、N 和 n。)

回答by kennytm

||means logical or. You want &&instead.

||表示逻辑或。你想要&&

if (userDecision != 'Y' && userDecision != 'y' ...


a || breturns true if eithera orb is true. Suppose the userDecisionis 'Y'. Then

a || b返回true,如果b为真。假设userDecision'Y'。然后

  • userDecision != 'Y'is false
  • userDecision != 'y'is true
  • userDecision != 'N'is true
  • userDecision != 'n'is true
  • userDecision != 'Y'是假的
  • userDecision != 'y'是真的
  • userDecision != 'N'是真的
  • userDecision != 'n'是真的

So together the condition is true and the if branch is executed.

因此,条件为真并且执行 if 分支。

OTOH, a && breturns true if botha andb are true, which is what you really need.

OTOH,a && b返回true,如果两者b是真实的,这是你真正需要的。

回答by zoul

Read the first part of the condition aloud: Is the choice different from Yor y? The problem is that any character is different either from Yor y. You've picked the wrong logical operator – if you want to be sure that user picked something else than those characters in the condition, you have to pick &&, logical and: Is the character different from Yandalso different from yandetc.

大声朗读条件的第一部分:选择与Y或不同y吗?问题是任何字符都不同于Yy。你找错逻辑运算符-如果你想确保用户拿起别的东西比条件下的人物,你必须选择&&,逻辑和:是从人物的不同Y,并还从不同的y等。

回答by Adriaan Stander

Change yoiur ORs to ANDs

将您的 OR 更改为 AND

Or you could use

或者你可以使用

(!(userDecision == 'Y' || userDecision == 'y' || userDecision == 'n' || userDecision == 'N'))

This

这个

!(A OR B) 

is equivelant to

相当于

!A AND !B

Have a look at Boolean algebra

看看布尔代数

De Morgans theorem

德摩根斯定理

NOT (P OR Q) = (NOT P) AND (NOT Q) 
NOT (P AND Q) = (NOT P) OR (NOT Q) 

DeMorgan's Theorem

德摩根定理

回答by rsp

Your condition is "if this is not a or not b" this means it will always be true even if it is a or b. What you want to test for is "if this is not (a or b)" so:

您的条件是“如果这不是 a 或不是 b”,这意味着即使它是 a 或 b,它也将始终为真。您要测试的是“如果这不是(a 或 b)”,因此:

if (! (userDecision == 'Y' || userDecision == 'y' || userDecision == 'n' || userDecision == 'N')) {
    System.out.println("Error: invalid input entered for the interstate question");
    System.exit(0);
}

If your code contains conditions like this that become long as many alternative chars must be tested for, you can use the switch construct, which makes this case easier to follow:

如果您的代码包含这样的条件,并且必须测试许多替代字符,那么您可以使用 switch 结构,这使得这种情况更容易理解:

switch (userDecision) {
case 'y': /*fallthrough*/
case 'Y':
    // accepted
    break;
case 'n': /*fallthrough*/
case 'N':
    // rejected
    break;

default:
    System.out.println("Error: invalid input entered for the interstate question");
    System.exit(0);
}