如果比较2个int的条件的语句以boolean运行

时间:2017-12-02 07:51:38

标签: java if-statement int boolean multiple-conditions

我的if语句无法正常工作。目的是将用户给定日期与当前日期进行比较,如果月份和周数都匹配(用户的生日),则getBonus = true。 但是,我的if语句给了我以下错误:

二元运算符'&&'的错误操作数类型 第一种类型:int 第二种类型:int

不兼容的类型:int无法转换为boolean

当所涉及的所有变量都是int时,为什么我的if语句试图作为布尔运行?

public static boolean getBonus ( int Week, int Month, int bMonth, int bWeek 
) {

boolean getBonus = false;

/**************************************************************************
The following statement is used to determine if the user's birthday is 
this week, using the month and week of the month. bMonth/bWeek are generated 
from user input, Month/Week are generated from a real time calendar.
**************************************************************************/

if(bWeek = Week && bMonth = Month)
  {
    getBonus = true;

  }

return getBonus;
}//end class getBonus

2 个答案:

答案 0 :(得分:0)

这样做

 if(bWeek == Week && bMonth == Month)//used to compare 
 // true && true --> true

而不是这个

 if(bWeek = Week && bMonth = Month)//used to assign 

答案 1 :(得分:0)

使用If Statement如下

if(bWeek == Week && bMonth == Month)
{
     getBonus = true;

 }

==用于比较

=用于分配值