2

我是一名大学新生,我开始学习我的第一门 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 ?????????`
}

编译器只会让我插入一个返回语句,并且 3 个问题中只有一个会被发送到下一个方法。我认为我需要一种方法来在退货声明中获取这三件事,这样程序就不会一遍又一遍地询问相同的收件人信息问题。

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

======================编辑=========================== ===== @TerryLi 通过在我的课堂上发现一个错误来帮助我

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

}

这就是我设法让它工作的方法。再次感谢@terryli 和所有帮助回复的人!

4

7 回答 7

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();
于 2013-02-28T19:58:49.730 回答
1

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

于 2013-02-28T20:00:53.333 回答
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 获取它们。(作为标准做法,据我所知)。希望能帮助到你。

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

于 2013-02-28T20:10:56.070 回答
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 方法来完成其他事情。

于 2013-02-28T20:17:09.270 回答
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);

}

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

于 2013-02-28T20:05:49.477 回答
0

两种方式。

  1. 使用全局变量

    **

    **

**

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

**

于 2013-02-28T20:07:19.237 回答
0

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

例如:

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);
}
于 2013-02-28T20:07:33.330 回答