2013-03-08 23 views
0

我不擅長編程,也不知道爲什麼這不起作用。無論我輸入什麼,它總是直接到else語句。我的Java程序不工作

public void pizzaIntro() 
    { 
    Scanner user_input = new Scanner(System.in); 
    String user_command = "null"; 
    String apology = "I'm sorry it appears there has been some kind of mistake in your order."; 
    System.out.println("Welcome to " + cM + " here we strive to deliver excellent services to all our customers!"); 
    System.out.println("The current prize for pizza is $" + bP + " and an extra $" + tP + " per topping."); 
    System.out.println(); System.out.println(); 
    while(user_command != "exit") 
    { 
     System.out.print("Would you like toppings?(yes/no):"); 
     user_command = user_input.next(); 
     if(user_command.toLowerCase() == "yes") 
     { 
      System.out.println("Good Eat Your Pizza."); 
     } 
     else if (user_command.toLowerCase() == "no") 
     { 
      System.out.println("Well Ok Then!"); 
     } 
     else 
     { 
      System.out.println(apology); 
      System.exit(1); 
     } 
    } 
    pic1.show(); 
} 
+2

使用.equals()代替== – smk 2013-03-08 05:46:26

+0

@SMK請回答這個問題... – MadProgrammer 2013-03-08 05:47:37

+0

唉!別人已經回答了。 – SudoRahul 2013-03-08 05:48:24

回答

2

使用equals方法比較字符串。要知道==和equals之間的區別,請閱讀https://stackoverflow.com/questions/7311451/difference-between-equals-and-instanceof 您將清楚在什麼時候使用什麼。

while(!(user_command.equals("exit")) { 
    System.out.print("Would you like toppings?(yes/no):"); 
    user_command = user_input.next(); 
    if(user_command.toLowerCase().equals("yes")) 
    { 
     System.out.println("Good Eat Your Pizza."); 
    } 
    else if (user_command.toLowerCase().equals("no")) 
    { 
     System.out.println("Well Ok Then!"); 
    } 
    else 
    { 
     System.out.println(apology); 
     System.exit(1); 
    } 
} 
1

使用equals方法,而不是==

user_command.toLowerCase().equals("yes") 

在Java中,==永遠只是比較兩個引用。它可以用於原始數據類型。字符串不是原始數據類型。字符串是一個對象。你應該使用equals方法。

在你的情況下,你可以考慮使用equalsIgnoreCase方法來忽略案例。

+0

而對於我們這些不好的人來說,請解釋一下爲什麼(這是對的,你必須爲那次投票工作;)) – MadProgrammer 2013-03-08 05:48:14

1

使用equalsIgnoreCase。它更安全

public void pizzaIntro() 
    { 
     Scanner user_input = new Scanner(System.in); 
     String user_command = "null"; 
     String apology = "I'm sorry it appears there has been some kind of mistake in your order."; 
     System.out.println("Welcome to " + cM 
          + " here we strive to deliver excellent services to all our customers!"); 
     System.out.println("The current prize for pizza is $" + bP 
          + " and an extra $" + tP + " per topping."); 
     System.out.println(); 
     System.out.println(); 
     while (!user_command.equalsIgnoreCase("exit")) 
     { 
      System.out.print("Would you like toppings?(yes/no):"); 
      user_command = user_input.next(); 
      if (user_command.equalsIgnoreCase("yes")) 
      { 
       System.out.println("Good Eat Your Pizza."); 
      } 
      else if (user_command.equalsIgnoreCase("no")) 
      { 
       System.out.println("Well Ok Then!"); 
      } 
      else 
      { 
       System.out.println(apology); 
       System.exit(1); 
      } 
     } 
     pic1.show(); 
    } 
1

永遠不要將任何Java對象與「==」(只有像int,long,double等原始類型)進行比較。它應該是:

if(user_command.toLowerCase().equals("yes")) { 
    ... 
} 

否則您檢查對象的位置是否相同,而不是內容。

在這個特殊的例子中,你可能只想使用String.equalsIgnoreCase(...)來代替。