2013-09-23 110 views
-1

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

非常感谢。

我PROGRAME shouild像下面这样进行:

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





} 

} 

回答

0

如果sideorder != 2那么变量potas根本没有被初始化,JVM不知道打印什么。

一个很好的解决方案与nullempty string像这样初始化:

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

非常感谢,初始化为null确实解决了问题。 – user234899

+0

@ user234899然后你可以接受答案。 – Claudiu

0

这条线:

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

声明了几个字符串,其中之一就是“POTAS”,但初始化为任何事情。

0

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

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

它也不会伤害初始化其他变量。