-1

我收到投诉说变量 potas 可能没有被初始化问题。我无法弄清楚这个错误。请问有什么建议吗?

非常感谢。

我的程序应该像下面这样执行:

Welcome to our steak house! I will now take your order.
Which steak can I get you?
please select:
1 T-Bone
2 Sirloin
3 Rib Eye
3
How do you want your steak cooked?
please select:
1 rare
2 medium rare
3 medium
4 medium done
5 done
? 4
What do you want on the side?
please select:
1 bread
2 potatoes
? 2
How do you want your potatoes?
please select:
1 fried
2 wedged
3 baked
? 3
Which topping do you want?
please select:
1 butter
2 French dressing
3 garlic
? 3
Thank you, you ordered: Rib Eye, medium done with baked potatoes and garlic topping.

Java代码:

public class EatingOut {

public static void main(String[] args) {

    int steakopt, steakcook, sideorder, potat, top;
    String steak, st1 ="T-Bone", st2 = "Sirloin", st3 = "Rib Eye";

    String steakc, stc1 = "rare", stc2 = "medium rare", stc3 ="medium", stc4 = "medium done", stc5 = "done";

    String topping, topping1 = "butter", topping2 ="French dressing", topping3 = "garlic";

    TextIO.put("Welcome to our steak house! I will now take your order.\n");
    TextIO.put("Which steak can I get you?\nplease select:\n");
    TextIO.put("1 T-Bone\n2 Sirloin\n3 Rib Eye\n");
    steakopt = TextIO.getlnInt();

    if (steakopt == 1)
        steak = st1;
    else if (steakopt == 2)
        steak = st2;
    else
        steak = st3;

    TextIO.put("How do you want your steak cooked?\nplease select:\n");     
    TextIO.put("1 rare\n2 medium rare\n3 medium\n4 medium done\n5 done");
    steakcook = TextIO.getlnInt();

    if (steakcook == 1)
        steakc = stc1;
    else if (steakcook == 2)
        steakc = stc2;
    else if(steakcook == 3)
        steakc = stc3;
    else if(steakcook == 4)
        steakc = stc4;
    else
        steakc = stc5;

    TextIO.put("What do you want on the side?\nplease select:\n1 bread\n2 potatoes");


    sideorder = TextIO.getlnInt();

    String potas, potopt1 = "fried", potopt2 = "wedged", potopt3 = "baked";

    String toside, side1="bread", side2 = "potatoes";


    if (sideorder == 2)
    {
        TextIO.putln("How do you want your potatoes?\nplease select:\n1 fried\n2 wedged\n3 baked\n");
        potat = TextIO.getlnInt();

        if (potat == 1)
            potas = potopt1;
        else if (potat == 2)
            potas = potopt2;
        else
            potas = potopt3;
    }
    else
        toside = side1;

    TextIO.putln("Which topping do you want?\nplease select:\n1 butter\n2 French dressing\n3 garlic");

    top = TextIO.getlnInt();

    if (top == 1)
        topping = topping1;
    else if (top == 2)
        topping = topping2;
    else
        topping = topping3;


    TextIO.putf("Thank you, you ordered: %s, %s with %s potatoes and %s topping.", steak,steakc,potas, topping );





}

}
4

3 回答 3

0

如果sideorder != 2然后变量potas根本没有初始化,jvm 不知道要打印什么。

一个好的解决方案是使用nullempty string像这样初始化它:

String potas=null, potopt1 = "fried", potopt2 = "wedged", potopt3 = "baked";
于 2013-09-23T20:55:55.647 回答
0

这一行:

String potas, potopt1 = "fried", potopt2 = "wedged", potopt3 = "baked";

声明了几个字符串,其中一个是“potas”,但初始化为任何内容。

于 2013-09-23T20:56:30.440 回答
0

就像听起来一样,您没有初始化potas。当您声明它时,您没有定义它是什么。

String potas = "", potopt1 = "fried", potopt2 = "wedged", potopt3 = "baked";

初始化其余的变量也没有什么坏处。

于 2013-09-23T20:56:35.217 回答