需要帮助在java方法中创建多个return语句

时间:2013-02-28 19:55:21

标签: java variables return

我是大学新生,我从我的第一个Java编程课程开始。教授向我们发送了一项任务,以便创建一个模拟USPS数据库的应用程序。

无论如何,我到了我希望我的程序逐行询问收件人信息(地址,姓名,城市等)的地步,我设法使用一个简单的JOptionPane。问题是,现在,我正在使用Java方法,我不能在我的生活中弄清楚如何制作return语句,以便我的程序继续使用我的收件人信息进入下一个方法。

private static String getString(String string) {
    // TODO Auto-generated method stub

    String nameString = JOptionPane.showInputDialog(null,
            "Recipients name:", "Certified Mail Receipt", 3);

    String streetAddressString = JOptionPane.showInputDialog(null,
            "Street, Apt. No. or P.O. Box No.:", "Certified Mail Receipt",
            3);

    String cityString = JOptionPane.showInputDialog(null,
            "City, State, Zip Code", "Certified Mail Receipt", 3);
    // return ?????????`
}

编译器只允许我插入一个return语句,并且只有3个问题中的一个被发送到下一个methood。我想我需要一种方法来在返回语句中获取这三个内容,以便程序不再一遍又一遍地询问相同的收件人信息问题。

如果有帮助,我正在使用Eclipse Java编译器。谢谢!

====================== EDIT ======================== ======== @TerryLi通过在班上发现错误来帮助我

private static String getString(String type) {
String result = JOptionPane.showInputDialog(null, 
        type, 
        "Certified Mail Receipt", 
        3);
return result;   

}

这就是我管理它的方式。再次感谢@terryli和所有帮助过回复的人!

7 个答案:

答案 0 :(得分:7)

编写一个包含数据的包装类:

public class SomeWrapper
{
    private String name;
    private String address;
    private String city;

    public SomeWrapper(String n, String a, String c)
    {
        name = n;
        address = a;
        city = c;
    }

    public String getAddress(){return address;}
    public String getName(){return name;}
    public String getCity(){return city;}
}

然后使用它:

private static String getData()
{
   // ...

   SomeWrapper w = new SomeWrapper(nameString, streetAddressString, cityString);

   return w;
}

并像这样提取它:

SomeWrapper w = getData();
String nameString = w.getName();
String streetAddressString = w.getAddress();
String cityString = w.getCity();

答案 1 :(得分:1)

您可以在此方法之外使用数组或静态变量。 一个方法可以很容易地返回包含你提到的所有三个字符串的字符串数组

答案 2 :(得分:0)

让我提供一个替代方案:

String splitter = "-.-";
return nameString+splitter+streetAddressString+splitter+cityString;

我们可以按如下方式检索返回值:

String returns = getString(...);
String nameString = returns.split(splitter)[0];
String streetAddressString = returns.split(splitter)[1];
String cityString = returns.split(splitter)[2];

如果您希望保持方法签名相同,这将是唯一的解决方法。

修改

鉴于您不熟悉Java,请尝试以下代码:

return nameString+"-.-"+streetAddressString+"-.-"+cityString;

基本上,我刚刚删除了全局变量splitter,以使其变得简单。

String nameString = returns.split("-.-")[0];
String streetAddressString = returns.split("-.-")[1];
String cityString = returns.split("-.-")[2];

<强>更新

您只需修改getString方法,如下所示:

private static String getString(String type) {
    String result = JOptionPane.showInputDialog(null, 
            type, 
            "Certified Mail Receipt", 
            3);
    return result;   
}

这应该可以解决您的问题。让我知道。

Update2

private static void showReceipt(double postage, double certifiedFee,
        double restrictedFee, double returnReceiptFee, String nameString,
        String addressString, String cityString) {
    // TODO Auto-generated method stub
    DecimalFormat fmt = new DecimalFormat(" $0.00");
    double certifiedMailFee = 0;
    double restrictedDelivery = 0;
    String outputString = "U.S. POSTAL SERVICE\nCERTIFIED MAIL RECEIPT\n---------------------\nPostage" + 
            fmt.format(postage) + 
            "\nCertified Mail Fee" + fmt.format(certifiedMailFee) + 
            "\nRestricted Delivery Fee" + fmt.format(restrictedDelivery) + 
            "\nReturn Receipt Fee" + fmt.format(returnReceiptFee) + 
            "\nTotal Postage & Fees" + fmt.format(postage + certifiedMailFee + restrictedDelivery + returnReceiptFee) + 
            "\n----------------------------" + 
            "\nSend to:" + 
            "\n" + nameString + 
            "\n----------------------------" + 
            "\nStreet, Apt No., or PO Box No." + 
            "\n" + addressString + 
            "\n----------------------------" + 
            "\nCity, State, ZIP+4®" + 
            "\n" + cityString;

    JOptionPane.showMessageDialog(null, 
            outputString, 
            "Certified Mail Receipt", 
            1, 
            null);

}

希望你会注意到我刚刚对上面代码所做的更改。

答案 3 :(得分:0)

两种方式。

  1. 使用Global Varaibles

    **

    **

  2. **

    private static String nameString, streetAddressString, cityString;
            private static String getString(String string) {
                // TODO Auto-generated method stub
    
                nameString = JOptionPane.showInputDialog(null,
                        "Recipients name:", "Certified Mail Receipt", 3);
    
                streetAddressString = JOptionPane.showInputDialog(null,
                        "Street, Apt. No. or P.O. Box No.:", "Certified Mail Receipt",
                        3);
    
                cityString = JOptionPane.showInputDialog(null,
                        "City, State, Zip Code", "Certified Mail Receipt", 3);
                //No need to return, they are available globally 
            }
    

    **

    **

    **

    或者,将它们作为方法参数传递

    **

    private static String getString(String string) {
        // TODO Auto-generated method stub
        String nameString = JOptionPane.showInputDialog(null,
                "Recipients name:", "Certified Mail Receipt", 3);
        String streetAddressString = JOptionPane.showInputDialog(null,
                "Street, Apt. No. or P.O. Box No.:", "Certified Mail Receipt",
                3);
        String cityString = JOptionPane.showInputDialog(null,
                "City, State, Zip Code", "Certified Mail Receipt", 3);
    
    stringMethod(nameString,streetAddressString,cityString);
    }
    

    **

答案 4 :(得分:0)

Java编程语言不允许返回多个变量。但是您可以返回对象,最准确地说,您可以返回对象的引用。因此,如果您想要返回多个内容,只需创建一个适合您所有需求的数据结构(类),并返回它的实例。

E.g:

public class DataStruct{

    public DataStruct(String nameString, String streetAddressString, String cityString){
         this.nameString = nameString;
         this.streetAddressString = streetAddressString;
         this.cityString = cityString;
    }

    public String nameString;
    public String streetAddressString;
    public String cityString;
}

private static String getString(String string) {
// TODO Auto-generated method stub

String nameString = JOptionPane.showInputDialog(null,
        "Recipients name:", "Certified Mail Receipt", 3);

String streetAddressString = JOptionPane.showInputDialog(null,
        "Street, Apt. No. or P.O. Box No.:", "Certified Mail Receipt",
        3);

String cityString = JOptionPane.showInputDialog(null,
        "City, State, Zip Code", "Certified Mail Receipt", 3);
return new DataStruct(nameString,streetAddressString,cityString);
}

答案 5 :(得分:0)

根据我的拙见,你有两个明显的选择,取决于你以后如何使用字符串。我看到它的方式,你可以返回一个连接的字符串,它返回所有变量的所有信息,在你的情况下将这样做:

/* The \n just puts each peace of info on a new line you could just do a 
" " instead or something. */
return nameString+"\n"+streetAddressString+"\n"+cityString;

或者你可以在某个地方(索引)返回一个字符串与每个字符串:

String[] stringList = new String[3];

将它们读入数组并返回数组(如果这样做,请不要忘记更改返回类型)。

/* index in arrays start at 0 so its always 1 less than the size you make it 
(if you didnt know) */
stringList[0] = nameString;
stringList.... etc.

此外,您应该知道getter通常是公共类,您只需将字段值设为私有,并使用getter获取它们。 (作为标准做法,据我所知)。 希望能帮助到你。

编辑:在我看来,当它甚至没有必要时使用静态变量,这是一个非常糟糕的主意。如果你可以在没有它的情况下解决问题,那就这样做吧。

答案 6 :(得分:0)

除非您使用数组或包装类或某种结构,否则Java不允许您返回多个值。既然已经涵盖了这两种方法,我建议将其分解为几种不同的方法,以使其更具可读性。

private static String promptForString(String message, String title)
{
    return JOptionPane.showInputDialog(null,message,title,3);
}

private static String promptForName()
{
    return promptForString("Recipients name:","Certified Mail Receipt");
}

private static String promptForStreetAddress()
{
    return promptForString("Street, Apt. No. or P.O. Box No.:","Certified Mail Receipt");
}

private static String promptForCity()
{
    return promptForString("City, State, Zip Code","Certified Mail Receipt");
}

然后在你的调用方法中你可以简单地使用

String name = promptForName();
String streetAddress = promptForStreetAddress();
String city = promptForCity();

这样做的另一个好处是允许您相当容易地添加更多字段,或者使用promptForString方法完全添加其他内容。