Java 如果车辆可用,则返回信息
我希望Java 如果车辆可用,则返回信息,java,Java,我希望getStatus方法返回一条消息,说明如果车辆处于null或已到达目的地,则车辆是空闲的。。但是我得到了一个错误,我不确定我的if语句出了什么问题。。我是编程新手,所以如果我的代码完全错误,我很抱歉 /** * Return the status of this Vehicle. * @return The status. */ public String getStatus() { return id + " at
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");
}
}
将始终返回true,因为=
用于assignmnet
使用==
进行比较
if (destination == null)
一旦你做了一个返回,它下面的任何东西都不会被执行。你需要在最后做返回。
地点=目的地
和
目的地=空
是错误的,因为=是赋值,您想要的是==这是相等的比较
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);
}
您的代码给出编译时错误不可访问语句。 目的地和位置应为同一类型
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;
}
我建议您阅读一些基本的java tutorial.if(location=destination)。是不是应该是==?这应该会给出编译时错误。因为它永远不会变成if语句。它是一个无法到达的语句。
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);
}
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;
}