2017-04-07 31 views
0

早上好/下午,正確使用Switch?菜單選項訂購三明治或沙拉

以下是我爲我的Java課(如在學校作業)所寫的代碼。我不是在這裏試圖讓某人爲我做作業。

我的問題是:可以使用開關/外殼如何設置它? 提示用戶輸入1或2.答案1他們得到一個三明治,回答2他們得到一份沙拉。

對這一計劃的說明與客戶開始將訂購三明治沙拉。然後繼續添加配料以獲得額外費用,並可以減少額外的配料。然後打印出訂單和總量。教授指定使用IF語句和Switch。然而,我有一種感覺,我錯過了一些東西,因爲它看起來像是我在Stack上找到的例子中的IF代替語句。感謝您的幫助!

import java.util.Scanner; 

//loaded scanner to take user input 
public class Restaurant 


//dude change this, do 3 outputs asking for input 1 input 2 input 3 make those if statements after 
{ 
/* Author: 
    Date: 
    Program: create a class that will offer a sandwich for $7.00 with optional three 
    toppings lettuce, tomato, cheese $1.00 each 
    or a salad with optional two toppings tomatos, cheese $0.50 each. 
*/ 
    public static void main(String[] args) 

    { 

    // Declarations sandwich,salad,Sandwich-cheese,Sandwich-tomato,Sandwich-lettuce, salad-cheese, salad-tomato. 
     double sandwich = 7.00; 
     double salad = 7.00; 
     double sandChe = 1.00; 
     double sandTom = 1.00; 
     double sandLet = 1.00; 
     double salChe = .50; 
     double salTom = .50; 
     int userInput; 
     int userInput1; 
     int userInput2; 
     int userInput3; 
     double sandTotal; 
     double saladTotal; 

     Scanner scanner = new Scanner(System.in); 

     System.out.println("Enter 1 for a Sandwich or 2 for a Salad"); 
     int userInput = scanner.nextLine(); 

     switch(userInput) 
     { 
     case 1: // a sandwich was ordered 




      System.out.println("Enter 1 for additional topping of lettuce or press 2"); 
      int userInput1 = scanner.nextLine(); 


      System.out.println("Enter 1 for additional topping of cheese or press 2"); 
      int userInput2 = scanner.nextLine(); 


      System.out.println("Enter 1 for additional topping of tomato or press 2"); 
      int userInput3 = scanner.nextLine(); 



      if (userInput1 == 1 && userInput2 == 1 && userInput3 == 1) 
      { 
       saladTotal = (sandwich + sandLet + sandChe + sandTom); 
       System.out.println("Your bill comes to a total of: " + sandTotal + " Thank you, Have a great day!"); 



       if (userInput1 == 1 && userInput2 == 2 && userInput3 == 2) 
       { 
        sandTotal = (sandwich + sandLet); 
        System.out.println("Your bill for a salad with additional tomato toppings comes too: " + sandTotal + " Thank you, Have a great day!"); 


        if (userInput1 == 1 && userInput2 == 1 && userInput3 == 2) 
        { 
        sandTotal = (sandwich + sandLet + sandChe); 
        System.out.println("Your bill for a salad with no additional toppings comes too: " + salad + " Thank you, Have a great day!"); 


        if (userInput1 == 1 && userInput2 == 2 && userInput3 == 1) 
        { 
         sandTotal = (sandwich + sandLet + sandTom); 
         System.out.println("Your bill for a sandwich `enter code here`lettuce and tomato comes too: " + sandTotal + " Thank you, Have a great day!");  


         if (userInput1 == 2 && userInput2 == 1 && userInput == 1) 
         { 
          sandTotal = (sandwich + sandChe + sandTom); 
          System.out.println("Your bill for a sandwich with cheese and tomato comes too: " + sandTotal + " Thank you, Have a great day!"); 


          if (userInput1 == 2 && userInput2 == 2 && userInput3 == 2) 
          { 
           System.out.println("Your bill for a sandwich comes too: " + sandwich + " Thank you, Have a great day!"); 
          }// end if 1 
         }//end if 2 
        }//end if 3 
        }//end if 4 
       }//end if 5 
      }//end if 6 

     } 
    } 
}    


     // switch case below 


     case 2: // a salad was ordered 




      { 
       System.out.println("Press 1 for additional topping of cheese or press 2"); 
       int userInput1 = scanner.nextLine(); 



       System.out.println("Press 1 for additional topping of tomato or press 2"); 
       int userInput2 = scanner.nextLine(); 


       if (userInput1 == 1 && userInput2 == 2) 
       { 
        saladTotal = (salad + salChe); 
        System.out.println("Your bill comes to a total of: " + saladTotal + " Thank you, Have a great day!"); 



        if (userInput1 == 2 && userInput2 == 1) 
        { 
        saladTotal = (salad + salTom); 
        System.out.println("Your bill for a salad with additional tomato toppings comes too: " + saladTotal + " Thank you, Have a great day!"); 


        if (userInput1 == 2 && userInput2 == 2) 
        { 
         System.out.println("Your bill for a salad with no additional toppings comes too: " + salad + " Thank you, Have a great day!"); 


         if (userInput1 == 1 && userInput2 == 1) 

         { 
          saladTotal = (salad + salChe + salTom); 
          System.out.println("Your bill for a salad with Tomato and Cheese toppings comes too: " + saladTotal + " Thank you, Have a great day!"); 

         }//end if 1 
        }//end if 2 
        }//end if 3 
       }//end if 4 


      } 



}// end of class 
+0

你測試了你的代碼嗎?因爲如果語句/代碼塊不符合你的想法。 if語句中的所有內容(從'{'到'}')只有在條件爲真時纔會發生,所以其他if語句都不會運行,因爲這些條件不能全部爲真。你爲什麼要築巢它們?不要嵌套它們。把它們放在彼此之下(在下一個if語句之前放置'} **,而不是在末尾)。 – Dukeling

+0

另外,是的,這是'switch'的一個恰當的用法,但是如果它只是您檢查的兩個條件,並且詢問工作代碼是否正確使用,您也可以只使用if語句[so]的話題,因爲這往往會歸結爲意見,這與[所以]努力成爲的不一致。 – Dukeling

+0

在第一個'switch'事件之後,我沒有看到'break;'因此可能出現錯誤,也嘗試改變它們而不是嵌套if'使用'switch'並替換當前的'switch'用'if'。這基本上是因爲一個'if'常用的時候很少選項,而一個'switch'的時候很多 – 3vts

回答

0

通常,switch-case語句和if/else塊是可以互換的。由於使用break(下面的例子),開關情況有點不同。儘管在大多數情況下使用if語句。我通常使用switch-case來處理基於枚舉的個案。

假設我們有一個枚舉食物類型:

public enum FoodType { 
    SANDWITCH, 
    SALAD, 
    FRUIT 
} 

所以,如果我想根據他選擇哪些食物詢問用戶的問題,我可能會做這樣的事情:

FoodType chosenType = FoodType.SANDWITCH; // can be user input 

switch (chosenType) { 
case SANDWITCH: 
    System.out.println("you want cheese?"); 
case SALAD: 
    System.out.println("Sauce?"); 
    break; 
case FRUIT: 
    System.out.println("which one?"); 
    break; 
default: 
    throw new IllegalStateException(); 
} 

請注意:

  • 當選擇SANDWITCH時,問題是否想要奶酪?和'醬?'被打印,因爲沒有break聲明。
  • 選擇FRUIT時,只有'哪一個?'將被打印。
  • default會發生,如果用戶選擇了別的東西,這不是SANDWITCH或SALAD或FRUIT。

    if (FoodType.SALAD.equals(chosenType)) {...} 
    

    當有少數情況下,我會評估者如果語句使用簡單:

你當然可以用if語句以及寫這個。此外,「案例」中的代碼不應過長/複雜。我想如果你想知道什麼時候使用哪種語句;有確定的一些非常好的博客文章或stackoverflow的答案。

//旁註:我知道在示例中default:是不可達的代碼。

+0

很好的例子和解釋!我相信我的困惑是圍繞教授要求使用switch和if語句。如果我使用Switch,案例「s」將非常漫長而複雜(至少對於我的技能)。如果我使用If語句,這將給出期望的結果,但省略使用開關,除非我用第一個選項,它有一個簡單的答案三明治或沙拉。謝謝您的意見! – Elements