交换机的正确用法?菜单选项订购三明治或沙拉



早上好/下午好,

下面是我为Java课编写的代码(就像在学校作业中一样)。我不是来找人帮我做功课的。

我的问题是:开关/外壳可以按照我的设置使用吗? 提示用户输入 1 或 2。答案 1 他们正在吃三明治,答案 2 他们正在吃沙拉。

该程序的说明从客户将订购三明治沙拉开始。然后继续添加浇头,但需额外付费,也可以拒绝额外的浇头。然后打印出订单和总计。教授指定使用 IF 语句和 Switch。但是,我有一种感觉,我错过了一些东西,因为看起来 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

一般来说,switch-case 语句和 if/else 块是可以互换的。由于使用了break,开关盒略有不同(下面的示例)。尽管在大多数情况下使用 if 语句。我通常使用开关案例来处理基于枚举的情况。

假设我们有一个枚举食物类型:

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陈述。
  • 选择水果时,只会打印"哪一个?
  • default,如果用户选择其他东西,而不是SANDWITCH,沙拉或水果。

当然,您也可以使用 if 语句编写此语句:

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

当情况很少时,我会使用简单的 if 语句进行评分。此外,"案例"中的代码不应过长/复杂。我想如果你想知道什么时候使用哪个语句;肯定有一些非常好的博客文章或StackOverflow答案。

旁注:我知道示例中default:代码不可访问。

最新更新