早上好/下午,正確使用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
你測試了你的代碼嗎?因爲如果語句/代碼塊不符合你的想法。 if語句中的所有內容(從'{'到'}')只有在條件爲真時纔會發生,所以其他if語句都不會運行,因爲這些條件不能全部爲真。你爲什麼要築巢它們?不要嵌套它們。把它們放在彼此之下(在下一個if語句之前放置'} **,而不是在末尾)。 – Dukeling
另外,是的,這是'switch'的一個恰當的用法,但是如果它只是您檢查的兩個條件,並且詢問工作代碼是否正確使用,您也可以只使用if語句[so]的話題,因爲這往往會歸結爲意見,這與[所以]努力成爲的不一致。 – Dukeling
在第一個'switch'事件之後,我沒有看到'break;'因此可能出現錯誤,也嘗試改變它們而不是嵌套if'使用'switch'並替換當前的'switch'用'if'。這基本上是因爲一個'if'常用的時候很少選項,而一個'switch'的時候很多 – 3vts