Java 运算符 || 参数类型未定义 boolean, int

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

The operator || is undefined for the argument type(s) boolean, int

javaif-statementboolean-logic

提问by user3294305

I have little problem with my Java code. I am using Dr.Java and it is giving me the error message that "The operator || is undefined for the argument type(s) boolean, int". If anyone could please

我的 Java 代码没什么问题。我正在使用 Dr.Java,它给我的错误消息是“运算符 || 未定义参数类型布尔型,int”。如果有人可以请

import java.util. Scanner;
public class Days

{ public static void main( String [] args)
  { Scanner in = new Scanner(System.in) ;
    System.out.print(" What month is it  ? " );
    int month= in.nextInt();
    System.out.print( " What day is it " );
    int day = in.nextInt( );




    **if( month == 1 || 2 || 3 )**
    {  System.out.print( " Winter" ) ;
    }
    else 
    {
      System.out.print( " Fall " ) ;
    }


}
}  

采纳答案by Jigar Joshi

month == 1 || 2 || 3

first part of expression would return booleanand you cannot ||booleanand int

表达式的第一部分将返回boolean,你不能||booleanint

change it to

将其更改为

if( month == 1 || month == 2 || month == 3 )

or

或者

if( month >= 1 &&  month <= 3 )

considering monthis int

考虑monthint

回答by Suresh Atta

Your syntax is wrong. Correct syntax is

你的语法是错误的。正确的语法是

if( month == 1 || month == 2 || month ==3 ) { .... }