我希望getStatus方法返回一条消息,说明如果车辆为null或已到达目的地,则该车辆是免费的。但是我得到了一个不兼容的错误类型,我不知道我的if语句有什么问题。我对编程很陌生,所以如果我的代码完全错误,我很抱歉。
/**
* Return the status of this Vehicle.
* @return The status.
*/
public String getStatus()
{
return id + " at " + location + " headed for " +
destination;
if (destination = null) {
System.out.println("This Vehicle is free");
}
else if (location=destination) {
System.out.println ("This Vehicle is free");
}
}发布于 2014-01-09 07:12:31
您的代码会给出无法到达的编译时错误语句。应该在同一类型的目的地和位置。
public String getStatus() {
if (destination == null) {
System.out.println("This Vehicle is free");
}
else if (location == destination) {
System.out.println ("This Vehicle is free");
}
return id + " at " + location + " headed for " + destination;
}发布于 2014-01-09 07:06:40
return id + " at " + location + " headed for " +
destination; // code after this statement is unreachable...发布于 2014-01-09 07:06:37
if(destination = null)将始终返回true,因为=用于转让人网。
使用==进行比较
if (destination == null) https://stackoverflow.com/questions/21013688
复制相似问题