首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果车辆可用,返回信息

如果车辆可用,返回信息
EN

Stack Overflow用户
提问于 2014-01-09 07:04:55
回答 4查看 64关注 0票数 0

我希望getStatus方法返回一条消息,说明如果车辆为null或已到达目的地,则该车辆是免费的。但是我得到了一个不兼容的错误类型,我不知道我的if语句有什么问题。我对编程很陌生,所以如果我的代码完全错误,我很抱歉。

代码语言:javascript
复制
 /**
     * 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");
            }

    }
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-01-09 07:12:31

您的代码会给出无法到达的编译时错误语句。应该在同一类型的目的地和位置。

代码语言:javascript
复制
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;
}
票数 1
EN

Stack Overflow用户

发布于 2014-01-09 07:06:40

代码语言:javascript
复制
return id + " at " + location + " headed for " +
           destination;   // code after this statement is unreachable...
  1. 你会得到无法到达的代码错误..。您的返回应该是执行的最后一条语句。
  2. 如果(目的地= null)是错误的.应该是if (目标== null)。分配值。“==”比较。
票数 1
EN

Stack Overflow用户

发布于 2014-01-09 07:06:37

代码语言:javascript
复制
if(destination = null)

将始终返回true,因为=用于转让人网。

使用==进行比较

代码语言:javascript
复制
if (destination == null) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21013688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档