如何在 Java 中使用“或”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20275572/
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 00:46:17  来源:igfitidea点击:

How to use 'or' in Java?

javaboolean-operations

提问by user2916424

I'm quite new to Java, and can't figure out how to use 'or'. What is the Java equivalent? I've already tried &&and ||but eclipse does not recognise it.

我对 Java 很陌生,无法弄清楚如何使用“或”。Java 等价物是什么?我已经尝试过了&&||但是 eclipse 无法识别它。

This is part of my code:

这是我的代码的一部分:

if (action.equals ("run") || ("sprint")) {
    System.out.println("you ran");
}
else {
    System.out.println("else");
}

回答by T.J. Crowder

I've already tried && and || but eclipse does not recognise it.

我已经尝试过 && 和 || 但 eclipse 无法识别它。

That's very strange, but just to cover the basics: Let's assume you have the variable aand it contains the value 5. Then:

这很奇怪,但只是为了涵盖基础知识:假设您有变量a并且它包含 value 5。然后:

if (a == 5 || a == 7)

...will be true, because the first part of the expression (a == 5) is true. So the statement "aequals 5 oraequals 7" is true.

...将是真的,因为表达式 ( a == 5)的第一部分是真的。所以“a等于 5a等于 7”的说法是正确的。

The ||operator can only be used, in Java, where a boolean (true or false) expression is expected, such as in an ifstatement like the above. So pretty much in an ifor a conditional operator (that ?...:thing, sometimes called the ternary operator).

所述||操作者仅可用于在Java中,其中布尔型(真或假)表达的预期,诸如在if象上面的语句。在 an ifor 条件运算符(那个?...:东西,有时称为三元运算符)中非常多。



Re your edit, the problem is that both sides of your ||operator aren't true or false ("boolean") expressions. Your statement:

重新编辑,问题在于您的||运算符的两边都不是真或假(“布尔”)表达式。你的声明:

if (action.equals ("run") || ("sprint")){

breaks down like this:

分解如下:

if (
   action.equals ("run")
|| // ("or")
   ("sprint")
)

the second part of that isn't a true/false, it's a string. The correct way to express that in Java (or nearly any other programming language) is:

第二部分不是真/假,它是一个字符串。在 Java(或几乎任何其他编程语言)中表达它的正确方法是:

if (action.equals ("run") || action.equals ("sprint")){

Now both sides of the ||result in true/false exprssions:

现在||结果的两边都是真/假表达式:

if (
   action.equals ("run")
|| // ("or")
   action.equals ("sprint")
)

The reason for this is that the second part may have nothing whatsoever to do with action, and so the compiler can't assume you mean to re-use it in the second part of the expression. You might, for instance, want to use ||with two completely unrelated things:

这样做的原因是第二部分可能与 无关action,因此编译器不能假设您打算在表达式的第二部分中重新使用它。例如,您可能想要使用||两个完全不相关的东西:

if (action.equals("run") || somethingElse.equals("run")) {

回答by Fabio Carello

Ok. ("sprint")is not a Boolean expression. Since a ifcondition expects a Boolean expression your code returns an error. You should change the line with:

好的。("sprint")不是布尔表达式。由于if条件需要布尔表达式,因此您的代码会返回错误。您应该将行更改为:

if (action.equals ("run") || action.equals("sprint")){

回答by StFS

The equalsmethod returns a boolean and the || operator wants two booleans on each side.

equals方法返回一个布尔值和 || 操作员需要每边两个布尔值。

You're doing an action.equals("run")on one side but then a ("sprint")on the other which isn't a boolean expression.

你在action.equals("run")一侧做一个,然后在另一侧做一个("sprint")不是布尔表达式的。

Change your code like so:

像这样更改您的代码:

if (action.equals("run") || action.equals("sprint")){

if (action.equals("run") || action.equals("sprint")){