简体   繁体   English

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

[英]Java: Checking contents of char variable with if condition

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并执行命令以退出程序,我在做什么错呢?

|| means logical or. 表示逻辑或。 You want && instead. 您需要&&代替。

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

a || b a || b returns true if either a or b is true. a || b返回true,如果A B是真实的。 Suppose the userDecision is 'Y' . 假设userDecision'Y' Then 然后

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

So together the condition is true and the if branch is executed. 因此,条件一起成立,并且执行if分支。

OTOH, a && b returns true if both a and b are true, which is what you really need. OTOH, a && b返回true,如果A B都是真实的,这是你真正需要的。

Read the first part of the condition aloud: Is the choice different from Y or y ? 大声朗读条件的第一部分:选择是否与Yy不同? The problem is that any character is different either from Y or y . 问题是任何字符都不同于Yy 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 Y and also different from y and etc. 你找错逻辑运算符-如果你想确保用户拿起别的东西比条件下的人物,你必须选择&& ,逻辑和:是从不同的字符Y 也不同y

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 德摩根定理

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. 您的条件是“如果它不是a或b”,这意味着即使它是a或b也将始终为真。 What you want to test for is "if this is not (a or b)" so: 您要测试的是“如果不是(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);
}

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. 您是在问这些字符中的“ none”是否匹配,而不是简单地问这四个字符是否匹配失败。 (A value cannot simultaneously be Y, y, N, and n.) (一个值不能同时是Y,y,N和n。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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