3
public class menucard 
{
    public static void main (String [] args)throws IOException
    {
        Scanner input= new Scanner(System.in);
        int tea=5,coffee=7,samosa=8,idly=15,biryani=50,talawa=35,item;
        System.out.print("   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY    :15\n5.BIRYANI :50\n6.TALAAWA :35\n \tHow many items you want to order:");
        int size=input.nextInt();

        for(int i=1;i<=size;i++)
        {
            System.out.print("Order Item No"+i+":");
            int choice=input.nextInt();
            switch(choice)
        {
        case 1:
            System.out.println("TEA : INR "+tea);
            break;
        case 2:
            System.out.println("COFFEE : INR "+coffee);
            break;
        case 3:
            System.out.println("SAMOSA : INR "+samosa);
            break;
        case 4:
            System.out.println("IDLY : INR "+idly);
            break;
        case 5:
            System.out.println("BIRYANI : INR "+biryani);
            break;
        case 6:
            System.out.println("TALAAWA : INR "+talawa);
            break;
            default:
            System.out.println("INVALID ENTRY");
        }

    }
    }
}

输出是:

MENU 
1.TEA     :5
2.COFFEE  :7
3.SAMOSA  :8
4.IDLY    :15
5.BIRYANI :50
6.TALAAWA :35
How many items you want to order:5
Order Item No1:3
SAMOSA : INR 8
Order Item No2:6
TALAAWA : INR 35
Order Item No3:1
TEA : INR 5
Order Item No4:3
SAMOSA : INR 8
Order Item No5:4
IDLY : INR 15

我需要通过添加这些项目的价格来生成账单。

我应该如何添加这些项目..?

4

7 回答 7

1

请不要使用 switch 语句!他们只会让你的生活变得艰难。

我想这样做:

public interface MenuCard {
    void addToMenuItems(MenuItem item);
    List<MenuItem> getMenuItems();
    void renderTo(PrintStream printStream);
}

public interface MenuItem {
    BigDecimal getPrice();
    String getDescription();
}

public interface Order {
    void add(MenuItem item);

    //total price of all ordered items
    BigDecimal getPrice();
}

public static void main(String[] args) {
    Scanner input= new Scanner(System.in);
    MenuCard menuCard = getMenuCard(); //from database or wherever it is stored
    menuCard.renderTo(System.out);
    Order order = createNewOrder();
    int itemIndex = input.nextInt();
    while(itemIndex > 0) {
        order.add(menuCard.getMenuItems().get(itemIndex-1));
        itemIndex = input.nextInt();
    }
    //now generate bill for order
}
于 2013-02-13T18:43:13.620 回答
0

您需要做的就是首先声明一个变量,比如total_amount

public class menucard 
{
public static void main (String [] args)throws IOException
{
    Scanner input= new Scanner(System.in);
    int tea=5,coffee=7,samosa=8,idly=15,biryani=50,talawa=35,item,total_amount=0;
    ............................................
    ........................................

现在在break语句之前的switch case中使用这个total_amount变量,即

switch(choice)
    {
    case 1:
        System.out.println("TEA : INR "+tea);
        total_amount = total_amount + tea;
        break;
    case 2:
        System.out.println("COFFEE : INR "+coffee);
        total_amount = total_amount +coffee;
        break;
.................
.................

其他情况也是如此。最后,在您的 for 循环之后,您可以打印总金额。谢谢

于 2013-02-07T06:16:27.877 回答
0

试试下面的代码:

    import java.io.IOException;
import java.util.Scanner;

public class menucard 
{
    public static void main (String [] args)throws IOException
    {
        int total =0;
        int item = 0;
        Scanner input= new Scanner(System.in);
        int tea=5,coffee=7,samosa=8,idly=15,biryani=50,talawa=35;
        System.out.print("   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY    :15\n5.BIRYANI :50\n6.TALAAWA :35\n \tHow many items you want to order:");
        int size=input.nextInt();

        for(int i=1;i<=size;i++)
        {
            System.out.print("Order Item No"+i+":");
            int choice=input.nextInt();
            switch(choice)
        {
            case 1:
            {
                System.out.println("TEA : INR "+tea);
                item++;
                total+=tea;
                display(item, total);
                break;
            }
            case 2:
            {
                System.out.println("COFFEE : INR "+coffee);
                item++;
                total+=coffee;
                display(item, total);
                break;
            }
            case 3:
            {

                System.out.println("SAMOSA : INR "+samosa);
                item++;
                total+=samosa;
                display(item, total);
                break;
            }
            case 4:
            {
                System.out.println("IDLY : INR "+idly);
                item++;
                total+=idly;
                display(item, total);
                break;
            }
            case 5:
            {
                System.out.println("BIRYANI : INR "+biryani);
                item++;
                total=+biryani;
                display(item, total);
                break;
            }
            case 6:
            {
                System.out.println("TALAAWA : INR "+talawa);
                item++;
                total=+talawa;
                display(item, total);
                break;
            }
                default:
                System.out.println("INVALID ENTRY");
            }

    }

    }

    public static void display(int item, int price)
    {
         System.out.println("Total bill for "+item+" = "+price+" INR  ");
    }
}
于 2013-02-07T06:01:06.893 回答
0
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    String s = "   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY    :15\n5.BIRYANI :50\n6.TALAAWA :35\n";
    int tea = 5, coffee = 7, samosa = 8, idly = 15, biryani = 50, talawa = 35;
    System.out
            .print("   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY    :15\n5.BIRYANI :50\n6.TALAAWA :35\n \tHow many items you want to order:");
    int size = input.nextInt();
    ArrayList<String> itemsList = new ArrayList<String>();
    ArrayList<Integer> price = new ArrayList<Integer>();
    for (int i = 1; i <= size; i++) {
        System.out.print("Order Item No" + i + ":");
        int choice = input.nextInt();
        switch (choice) {
        case 1:

            System.out.println("TEA : INR " + tea);
            itemsList.add("Tea");
            price.add(tea);
            System.out.println(s);
            break;
        case 2:
            System.out.println("COFFEE : INR " + coffee);
            itemsList.add("Coffee");
            price.add(coffee);
            System.out.println(s);
            break;
        case 3:
            System.out.println("SAMOSA : INR " + samosa);
            itemsList.add("Samosa");
            price.add(samosa);
            System.out.println(s);
            break;
        case 4:
            System.out.println("IDLY : INR " + idly);
                            itemsList.add("Idly");
            price.add(idly);
            System.out.println(s);
            break;
        case 5:
            System.out.println("BIRYANI : INR " + biryani);
            itemsList.add("Biryani");
            price.add(biryani);
            System.out.println(s);
            break;
        case 6:
            System.out.println("TALAAWA : INR " + talawa);
            itemsList.add("Talawa");
            price.add(talawa);
            System.out.println(s);
            break;
        default:
            System.out.println("INVALID ENTRY");
        }

    }
    int total = 0;
    System.out.println("Item        Price");
    for (int i = 0; i < itemsList.size(); i++) {
        System.out.print(itemsList.get(i) + "       ");
        System.out.println(price.get(i));
        total = total + price.get(i);

    }
    System.out.println("Total       " + total);
}

输出:

        MENU 
1.TEA     :5
2.COFFEE  :7
3.SAMOSA  :8
4.IDLY    :15
5.BIRYANI :50
6.TALAAWA :35
    How many items you want to order:3
Order Item No1:1
TEA : INR 5
   MENU 
1.TEA     :5
2.COFFEE  :7
3.SAMOSA  :8
4.IDLY    :15
5.BIRYANI :50
6.TALAAWA :35

Order Item No2:2
COFFEE : INR 7
   MENU 
1.TEA     :5
2.COFFEE  :7
3.SAMOSA  :8
4.IDLY    :15
5.BIRYANI :50
6.TALAAWA :35

Order Item No3:3
SAMOSA : INR 8
   MENU 
1.TEA     :5
2.COFFEE  :7
3.SAMOSA  :8
4.IDLY    :15
5.BIRYANI :50
6.TALAAWA :35

Item        Price
Tea         5
Coffee      7
Samosa      8
Total       20

我编辑了代码以在每个案例执行后打印菜单并以一种好的方式生成账单。

于 2013-02-07T06:13:47.687 回答
0

这是一个switch-case加号enum

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Menucard {
     private static enum Item {
        TEA (5,"Tea"), COFFEE(7,"Coffee"), SAMOSA(8,"Samosa"), IDLY(15,"Idly"), BIRYANI(50,"Biryani"), TALAWA(35,"Talawa");
        private int price;
        private String name;
        private Item(int price, String name) {
            this.price = price;
            this.name = name;
        }
        public int getPrice() {
            return price;
        }
        public String getName(){
            return name;
        }
     };
    public static void main (String [] args)throws IOException
    {
        Scanner input= new Scanner(System.in);
        List<Item> itemList = new ArrayList<Item>();
        float totalAmt = 0;
        System.out.print("   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY    :15\n5.BIRYANI :50\n6.TALAAWA :35\n \tHow many items you want to order:");
        int size=input.nextInt();
        for(int i=1;i<=size;i++)
        {
            System.out.print("Order Item No"+i+":");
            int choice=input.nextInt();
            Item selectedItem = null;
            switch(choice)
            {
            case 1:
                selectedItem = Item.TEA;
                break;
            case 2:
                selectedItem = Item.COFFEE;
                break;
            case 3:
                selectedItem = Item.SAMOSA;
                break;
            case 4:
                selectedItem = Item.IDLY;
                break;
            case 5:
                selectedItem = Item.BIRYANI;
                break;
            case 6:
                selectedItem = Item.TALAWA;
                break;
            default:
                System.out.println("INVALID ENTRY");
            }
            if(selectedItem != null){
                itemList.add(selectedItem);
                System.out.println(selectedItem.getName() + " Price INR: " + selectedItem.getPrice());
                totalAmt += selectedItem.getPrice();
            }
        }
        System.out.println("Total Bill: " + totalAmt);
    }
}

这个没有switch-case

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Menucard {
     private static enum Item {
        TEA (5,"Tea"), COFFEE(7,"Coffee"), SAMOSA(8,"Samosa"), IDLY(15,"Idly"), BIRYANI(50,"Biryani"), TALAWA(35,"Talawa");
        private int price;
        private String name;
        private Item(int price, String name) {
            this.price = price;
            this.name = name;
        }
        public int getPrice() {
            return price;
        }
        public String getName(){
            return name;
        }
     };
    public static void main (String [] args)throws IOException
    {
        Scanner input= new Scanner(System.in);
        List<Item> itemList = new ArrayList<Item>();
        float totalAmt = 0;
        System.out.print("   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY    :15\n5.BIRYANI :50\n6.TALAAWA :35\n \tHow many items you want to order:");
        int size=input.nextInt();
        for(int i=1;i<=size;i++)
        {
            System.out.print("Order Item No"+i+":");
            int choice=input.nextInt();
            Item selectedItem = null;
            if(choice < 1 || choice > 6){
                System.out.println("Invalid selection");
                i--;
                continue;
            }
            selectedItem = Item.values()[choice-1];
            if(selectedItem != null){
                itemList.add(selectedItem);
                System.out.println(selectedItem.getName() + " Price INR: " + selectedItem.getPrice());
                totalAmt += selectedItem.getPrice();
            }
        }
        System.out.println("Total Bill: " + totalAmt);
    }
}
于 2013-02-07T09:03:00.030 回答
0
int sum = 0;
for(int i=1;i<=size;i++)
        {
            System.out.print("Order Item No"+i+":");
            int choice=input.nextInt();
            switch(choice)
        {
        case 1:
            System.out.println("TEA : INR "+tea);
            sum +=tea;
            break;
        case 2:
            System.out.println("COFFEE : INR "+coffee);
             sum +=coffee;
            break;
... etc

    }
}

sum 应该是总数

于 2013-02-07T05:56:44.517 回答
0

只需获取一个全局静态变量,将其初始化为零并在每个案例语句中继续为其添加价格值

public static int totalAmount=0;

在你的使用这个

public class menucard 
{
public static void main (String [] args)throws IOException
{
public static long totalAmount=0;
    Scanner input= new Scanner(System.in);
    int tea=5,coffee=7,samosa=8,idly=15,biryani=50,talawa=35,item;
    System.out.print("   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY            :15\n5.BIRYANI :50\n6.TALAAWA :35\n \tHow many items you want to order:");
    int size=input.nextInt();

    for(int i=1;i<=size;i++)
    {
        System.out.print("Order Item No"+i+":");
        int choice=input.nextInt();
        switch(choice)
    {
    case 1:
        System.out.println("TEA : INR "+tea);
totalAmount+=tea;
        break;
    case 2:
        System.out.println("COFFEE : INR "+coffee);
totalAmount+=tea;
        break;
    case 3:
        System.out.println("SAMOSA : INR "+samosa);
totalAmount+=tea;
        break;
    case 4:
        System.out.println("IDLY : INR "+idly);
totalAmount+=tea;
        break;
    case 5:
        System.out.println("BIRYANI : INR "+biryani);
totalAmount+=tea;
        break;
    case 6:
        System.out.println("TALAAWA : INR "+talawa);
totalAmount+=tea;
        break;
        default:
        System.out.println("INVALID ENTRY");
}

}

System.out.println("Total Amount is "+totalAmount);
}
于 2013-12-02T14:44:23.950 回答