有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java对交换机的正确使用?菜单选项点三明治或沙拉

早上好/下午好

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

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

本程序的说明始于客户将订购一份三明治或一份沙拉。然后继续添加额外费用的浇头,也可以拒绝额外的浇头。然后打印出订单和总数。教授指定使用IF语句和开关。然而,我有一种感觉,我遗漏了一些东西,因为它看起来像是我在堆栈上找到的示例中的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) 个答案