缺少return语句错误

时间:2012-07-08 23:51:36

标签: java return

我已经编写了一个代码,每当我尝试测试它时,我都会收到错误消息:

"StreetAddress.java:62: missing return statement 

 }"

以下是代码:

public class StreetAddress
  {
     private String myStreet;
     private String myCity;
     private String myState;
     private String myZip;

 public StreetAddress( String street, String city, String state, String zip )
 {
  myStreet = street;
  myCity = city;
  myState = state;
  myZip = zip;
 }

 public void setStreet( String street )
{
 myStreet = street;
}

 public String getStreet()
{
 return myStreet;
}

 public void setCity( String city )
{
 myCity = city;
}

 public String getCity()
{
 return myCity;
}

 public void setState( String state )
{
 myState = state;
}

 public String getState()
{
 return myState;
}

 public void setZIP( String zip )
{
 myZip = zip;
}

 public String getZIP()
{
 return myZip;
}

public String mailingLabel()
{
 System.out.println( getStreet() );
 System.out.println( getCity() + ", " + getState() + " " + getZIP() );
}
}
}

我不知道在哪里丢失}我多次检查过我的代码。请帮忙!

2 个答案:

答案 0 :(得分:9)

此功能:

public String mailingLabel()
{
 System.out.println( getStreet() );
 System.out.println( getCity() + ", " + getState() + " " + getZIP() );
}
声明

返回一个字符串,但缺少一个return语句。要么使它无效,要么返回一些东西 - 无论是什么意图。

答案 1 :(得分:1)

public String mailingLabel()
{
 System.out.println( getStreet() );
 System.out.println( getCity() + ", " + getState() + " " + getZIP() );
}

在上面的方法中,你应该根据方法的签名返回一个String,但是你没有返回String。

2个选项:

一个。保持方法签名相同,返回String:

public String mailingLabel()
{
 return getStreet() + getCity() + ", " + getState() + " " + getZIP();
}

OR

B中。更改方法签名:

public void mailingLabel()
{
 System.out.println( getStreet() );
 System.out.println( getCity() + ", " + getState() + " " + getZIP() );
}