Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java if和| |问题_Java_If Statement_Boolean_Jgrasp - Fatal编程技术网

Java if和| |问题

Java if和| |问题,java,if-statement,boolean,jgrasp,Java,If Statement,Boolean,Jgrasp,我正在做这个小程序,但不幸的是我遇到了这个问题 if (ccnString.charAt(0) != '4' || ccnString.charAt(0) != '3') { System.out.println("The String entered does not fit any of the Credit card standards"); System.exit(0); } 我的程序无法识别我是否在字符串中输入了任何整数 但是,如果删除| |和最后一

我正在做这个小程序,但不幸的是我遇到了这个问题

  if (ccnString.charAt(0) != '4' || ccnString.charAt(0) != '3') { 
      System.out.println("The String entered does not fit any of the Credit card standards");
      System.exit(0);
  }
我的程序无法识别我是否在字符串中输入了任何整数

但是,如果删除| |和最后一部分,if语句将识别第一个整数

我错过了什么

你可能想要

if (ccnString.charAt(0) != '4' && ccnString.charAt(0) != '3') {
    System.out.println("The String entered does not fit any of the Credit card standards");
    System.exit(0);
}
这将仅为不以
4
开头且不以
3
开头的字符串提供错误消息

对于不是以
4
开头或不是以
3
开头的任何字符串,原始条件都会给出一个错误,并且由于所有字符串都满足该条件,因此您将始终收到错误消息

如果初始测试后需要附加条件,可以执行以下操作:

if (ccnString.charAt(0) != '4' && ccnString.charAt(0) != '3') {
    System.out.println("The String entered does not fit any of the Credit card standards");
    System.exit(0);
} else if (ccnString.charAt(0) == '3' && ccnString.charAt(1) == '7') {
     // here you know that ccnString starts with 37
} else if (...) {
    ...
}
... add as many else ifs as you need ...
else {
    // default behavior if all previous conditions are false
}

它应该是
&&
而不是
|

ccnString.charAt(0) != '4' && ccnString.charAt(0) != '3'
Else
(ccnString.charAt(0)!=“4”| | ccnString.charAt(0)!=“3”
始终
true

if (ccnString.charAt(0) != '4' || ccnString.charAt(0) != '3')
始终为

每个字符都是
!=“4”或
!=“3”

我想你想要的是
&&

详情:

如果A为真或B为真(或两者均为真),则语句
A | | B
为真

在您的示例中,假设第一个字符是“4”

A=
ccnString.charAt(0)!=“4”为假(4!=4为假)

B=
ccnString.charAt(0)!=“3”为真(3!=4为真)


因此
A | | B
是真的,因为B是真的。

这是对许多其他答案的补充,这些答案正确地指出您必须使用and(
&&&
)而不是or(
|

 if ((ccnString.charAt(0) != '4' && ccnString.charAt(0) != '3')
                || (ccnString.charAt(0) == '3' && ccnString.charAt(1) == '7')) {
            System.out.println("The String entered does not fit any of the Credit card standards");
            System.exit(0);
        }
你被他们愚弄了。他们定义了布尔表达式的求反方式

在您的示例中,定义有效用户输入的原始表达式如下所示:

validInput = userPressed3 or userPressed4
但由于我们对无效用户输入感兴趣,因此必须否定此表达式:

not(validInput) = not(userPressed3 or userPressed4)
根据De Morgan的说法,
not(A或B)
等于
not(A)和not(B)
。因此我们还可以写:

not(validInput) = not(userPressed3) and not(userPressed4)

换句话说:这是德·摩根的错!;)

也许你在寻找和,而不是或。关于你所指定的东西。选择不同的字符(例如,
x
3
4
)并思考
|
的每一面的计算结果。有人能解释一下吗?我的程序无法识别我是否在字符串中输入了任何整数。这是什么意思?这是否意味着如果我们有35个或45个,一切都可以?或者我们必须有34个或43个?@霍布斯,你能解释得更清楚些吗?德·摩根又敲了…;)这就解决了问题。。但是,我还要检查相同的if语句,如果前两位也是37。在这种情况下,&&会有缺陷,不是吗?@Avacay你可以在原始if之后的else-if(){}语句中添加下一个条件。@Avacay回答已发布,这是你想要的,如果(ccnString.charAt(0)='3'&&ccnString.charAt(1)='7'){}没有给我想要的东西。。我仍然可以像67一样做,它的回声是正确的(@Avacay如果你输入67,第一个条件(如我的回答)应该给你一条错误消息。我很难理解。这怎么总是真的?@KickButtowski A | | B表示如果A或B为真,那么整体结果为真,而A&&B表示只有当A和B都为真时,整体结果才为真true@KickButtowski:如果第一个字符为“4”(这是第一部分返回
false
)的唯一方法,那么它必然不等于“3”。@BobTheBuilder因此,如果我们将!=更改为==,则更好?如果字符串以3或4开头,它将显示错误消息。