Java类型错误
问题描述:
我得到错误类型的非法开始在第一个嵌套的if语句,非法开始有看着类似的问题,我理解这通常是由一个错位的支柱引起的,不过,我看不到一个。谁能帮忙?Java类型错误
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;
}
}
}
答
您需要使用==
测试平等。 =
是赋值运算符。
甲骨文公司拥有良好的tutorial docs解释Java的运营商。
在相关说明中,Object.equals()
和==
之间也有一个重要的区别,这在其他堆栈溢出问题(如this one)中有详细介绍。但是,比较您正在做的基本类型时,==
就是您想要的。
答
应该是:
if (journeyType == 1){
表达,如果块应该总是导致布尔值(无论是真实的(或)假)。
非常感谢主席先生。 – seanysull 2012-04-03 17:47:33