我在第一个嵌套的if语句中得到了非法的类型错误启动,看了类似的问题我明白这通常是由错位的括号引起的,但是,我看不到一个。有人可以帮忙吗?
public class Journey
{
private int date;
private double time;
private int busNumber;
private int journeyType;
public static double dayCharge;
public static final double maxDayCharge = 3.50;
public static double weekCharge;
public static final double maxWeekCharge = 15;
public static double monthCharge;
public static final double maxMonthCharge = 48;
private int journeyId;
private static int numberOfJourneys;
private double costOfJourney;
public int getDate(){
return date;
}
public Double getTime(){
return time;
}
public int getBusNumber(){
return busNumber;
}
public double journeyCost(journey reqJourney){
if (journeyType = 1){ //this is where the error occurs
if (dayCharge =< 2.50)
{
costOfJourney = 1;
}
else
{
costOfJourney = maxDayCharge-dayCharge;
}
}
else if (journeyType = 2)
{
if (dayCharge =< 1.80)
{
costOfJourney = 1.70;
}
else
{
costOfJourney = maxDayCharge-dayCharge;
}
}
else if (journeyType = 3)
{
if (dayCharge =< 1.60)
{
costOfJourney = 1.90;
}
else
{
costOfJourney = maxDayCharge-dayCharge;
}
}
return costOfJourney;
}
public boolean isInSequence(journey reqJourney){
journey prevJourney = jArray.get(jArray.size()-1);
if (prevJourney.date > reqJourney.date)
{
return false;
}
}
}
答案 0 :(得分:1)
您需要使用==
来测试相等性。 =
是赋值运算符。
Oracle有很好的tutorial docs解释Java运算符。
在相关的说明中,Object.equals()
和==
之间也存在重要区别,其他Stack Overflow问题(如this one)中详细介绍了这一点。但是,在比较您正在进行的原始类型时,==
就是您想要的。
答案 1 :(得分:1)
应该是:
if (journeyType == 1){
if块中的表达式应始终导致布尔值(true(或)false)。