0

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

"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() );
}
}
}

我不知道哪里缺少 } 我检查了我的代码好几次。请帮忙!

4

2 回答 2

9

这个功能:

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

被声明为返回一个字符串,但缺少一个返回语句。要么让它无效,要么返回一些东西——不管是什么意图。

于 2012-07-08T23:54:32.557 回答
1
public String mailingLabel()
{
 System.out.println( getStreet() );
 System.out.println( getCity() + ", " + getState() + " " + getZIP() );
}

在上述方法中,您应该根据方法的签名返回一个字符串,但您没有返回字符串。

2个选项:

A. 保持方法签名相同,返回字符串:

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

或者

B. 更改方法签名:

public void mailingLabel()
{
 System.out.println( getStreet() );
 System.out.println( getCity() + ", " + getState() + " " + getZIP() );
}
于 2012-08-06T22:10:54.987 回答