我想问我是否以正确的方式编写此代码

时间:2019-05-05 21:36:13

标签: java while-loop

因此,基本上我必须创建一个菜单,该菜单将要求用户选择食物,然后继续执行该操作,直到按下六个为止。另外,我必须总结用户必须付出的钱,并增加8%的税。这是格式 声明变量 输出菜单 循环启动(不要将菜单项编号用于循环条件) 询问物品编号 启动开关语句 每个案例应该看起来像这样 要求数量 确定订购商品的价格 累计账单小计 累计订购商品的总数量 默认案例声明-更改循环变量以打破循环 找出税收和总计 输出项目总数,小计,税项和总计

    import java.util.Scanner;
    public class lab8a{
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //This is the menu that you choose from
      Int sum=0;

         System.out.println("1- Coffee and Donut- $3.50");
        System.out.println("2- Coffee and Bagel- $3.75");
        System.out.println("3- Coffee, Donut and Juice- $4.00");
        System.out.println("4- 1/2 dozen donuts- $5.00");
        System.out.println("5- 1 dozen donuts- $7.50");
    Scanner input1= new Scanner(System.in);
    System.out.print(“Enter 6 to exit or any other # to continue);
   Int cont=input.nextInt();
    System.out.print();          

    while(cont!=6){ 

    Scanner input= new Scanner(System.in);//creates scanner for menu
    System.out.println("choose the number of the order");//order based on 
    integer input
    int menu= input.nextInt();
    System.out.println();//prints out # chosen

    double menu3= menu2;//switches the menu2 to a double to do math with decimals

            switch (menu) {

            case 1:
    Scanner input2= new Scanner(System.in);//second scanner for quantity
    System.out.println("How Many?");
            int item1= input.nextInt();
            System.out.println();//prints out # of orders
       System.out.print("You are ordering " + item1+ " orders of coffee and donut for a total of $" + 3.50*item1);//prints out order and total
            System.out.println("");
        sum+=3.50*item1;
        break;//break for cases
        case 2:
Scanner input3= new Scanner(System.in);//second scanner for quantity
        System.out.println("How Many?");
        int item2= input.nextInt();
        System.out.println();//prints out # of orders
     System.out.print("You are ordering " + item2+ " orders of coffee and bagel for a total of $" + 3.75*item4);
        System.out.println("");
    s+=3.75*item2;
            break;
            case 3: 
    Scanner input4= new Scanner(System.in);//second scanner for quantity
            System.out.println("How Many?");
            int item3= input.nextInt();
        System.out.println();//prints out # of orders
System.out.print("You are ordering " + item3+ " orders of Coffee, Donut, and Juice for a total of $" + 4.00*item3);
        System.out.println("");
s+=4.00*item3
        break;
        case 4:
Scanner input5= new Scanner(System.in);//second scanner for quantity
        System.out.println("How Many?");
        int item4= input.nextInt();
        System.out.println();//prints out # of orders
 System.out.print("You are ordering " + item4+ " orders of 1/2 dozen donuts for a total of $" + 5.00*item4);
        System.out.println("");
s+=5.00*item4;
        break;
        case 5: 
Scanner input6= new Scanner(System.in);//second scanner for quantity
        System.out.println("How Many?");
        int item5= input.nextInt();
        System.out.println();//prints out # of orders
System.out.print("You are ordering " + item5+ " orders of 1 dozen donuts for a total of $" + 7.00*item5);
        System.out.println("");
s+=7.00*item5;
        Break;

 }
System.out.print(“Your subtotal is “ +sum);
System.out.println(“Your total is” + sum*1.08);
 }


so what is supposed to happen is that I ask the user what they want to 
order and continue asking them. Also I have to add up the price of the 
orders and output the overall price when the user exits

1 个答案:

答案 0 :(得分:0)

尝试一下:

import java.util.Scanner;
public class lab8a {
public static void main(String[] args) {
    // TODO Auto-generated method stub
    //This is the menu that you choose from
    Scanner input = new Scanner(System.in);
    double sum = 0;
    int choice;

    do{
        System.out.println();
    System.out.println("1- Coffee and Donut- $3.50");
    System.out.println("2- Coffee and Bagel- $3.75");
    System.out.println("3- Coffee, Donut and Juice- $4.00");
    System.out.println("4- 1/2 dozen donuts- $5.00");
    System.out.println("5- 1 dozen donuts- $7.50");
    System.out.println("6- Exit");
    choice = input.nextInt();
    System.out.println("Your Choice is: "+choice);//prints out # chosen
        switch (choice) {

            case 1:
                System.out.println("How Many?");
                int item1 = input.nextInt();
                System.out.println();//prints out # of orders
                System.out.print("You are ordering " + item1 +
                        " orders of coffee and donut for a total of $" + 3.50 * item1);
                //prints out order and total
                System.out.println("");
                sum += 3.50 * item1;
                break;//break for cases
            case 2:
                System.out.println("How Many?");
                int item2 = input.nextInt();
                System.out.println();//prints out # of orders
                System.out.print("You are ordering " + item2 + " orders of coffee and bagel for a total of $" + 3.75 * item2);
                System.out.println("");
                sum+= 3.75 * item2;
                break;
            case 3:
                System.out.println("How Many?");
                int item3 = input.nextInt();
                System.out.println();//prints out # of orders
                System.out.print("You are ordering " + item3 + " orders of Coffee, Donut, and Juice for a total of $" + 4.00 * item3);
                System.out.println("");
                sum+= 4.00 * item3;
                break;
            case 4:
                System.out.println("How Many?");
                int item4 = input.nextInt();
                System.out.println();//prints out # of orders
                System.out.print("You are ordering " + item4 + " orders of 1/2 dozen donuts for a total of $" + 5.00 * item4);
                System.out.println("");
                sum+= 5.00 * item4;
                break;
            case 5:
                System.out.println("How Many?");
                int item5 = input.nextInt();
                System.out.println();//prints out # of orders
                System.out.print("You are ordering " + item5 + " orders of 1 dozen donuts for a total of $" + 7.50 * item5);
                System.out.println("");
                sum+= 7.50 * item5;
                break;


        }
        System.out.println("Your subtotal is " + sum);
        System.out.println("Your total is " + sum * 1.08);
    }while (choice!=6);
 }
}
相关问题