编写一个超市购物程序,在一家超市有牙刷、毛巾、水杯、苹果和香蕉五种商品,商品价格如下表所示。

import java.util.Scanner;//导入


public class text1 {
    static double toothbrush = 8.8;//牙刷价格
    static double towel = 10.0;//毛巾价格
    static double cup = 18.8;//水杯价格
    static double apple = 12.5;//苹果价格
    static double banana = 15.5;//香蕉价格

    public static void main(String[] args) {
        System.out.println("--------黑吗小商城-------");
        System.out.println("1、牙刷的价格为:"+toothbrush+"元");
        System.out.println("2、毛巾的价格为:"+towel+"元");
        System.out.println("3、水杯的价格为:"+cup+"元");
        System.out.println("4、苹果的价格为:"+apple+"元");
        System.out.println("5、香蕉的价格为:"+banana+"元");
        String Flag1="Y";
        while(Flag1.equals("Y")) {                     //嵌套
            System.out.println("请输入你需要购买的商品序列号");
            Scanner sc = new Scanner(System.in);       //通过scanner类让用户输入序列号及购买数量
            int idx = sc.nextInt();
            switch (idx) {                             //多分支
                case 1:
                     System.out.println("请输入你需要购买牙刷的数量:");
                     int shu1 = sc.nextInt();              //获取整型数值的输入
                     double sum1 = shu1 * toothbrush;
                     System.out.println("你购买了牙刷" + shu1 + "支,需要花费" + sum1 + "元");
                     System.out.println("需要继续购买请输入Y,否则输入N");
                     Flag1 = sc.next();
                     break;
                case 2:
                    System.out.println("请输入你需要购买毛巾的数量:");
                    int shu2 = sc.nextInt();
                    double sum2 = shu2 * towel;
                    System.out.println("你购买了毛巾" + shu2 + "条,需要花费" + sum2 + "元");
                    System.out.println("需要继续购买请输入Y,否则输入N");
                    Flag1 = sc.next();
                    break;
                case 3:
                   System.out.println("请输入你需要购买水杯的数量:");
                   int shu3 = sc.nextInt();
                   double sum3 = shu3 * cup;
                   System.out.println("你购买了水杯" + shu3 + "个,需要花费" + sum3 + "元");
                   System.out.println("需要继续购买请输入Y,否则输入N");
                   Flag1 = sc.next();
                   break;
                case 4:
                   System.out.println("请输入你需要购买苹果的数量:");
                   int shu4 = sc.nextInt();
                   double sum4 = shu4 * apple;
                   System.out.println("你购买了苹果" + shu4 + "个,需要花费" + sum4 + "元");
                   System.out.println("需要继续购买请输入Y,否则输入N");
                   Flag1 = sc.next();
                   break;
                case 5:
                   System.out.println("请输入你需要购买香蕉的数量:");
                   int shu5 = sc.nextInt();
                   double sum5 = shu5 * banana;
                   System.out.println("你购买了香蕉" + shu5 + "个,需要花费" + sum5 + "元");
                   System.out.println("需要继续购买请输入Y,否则输入N");
                   Flag1 = sc.next();
                   break;

                default:
                    System.out.println("无此商品,请重新输入!");
                    break;
            }
        }
        System.out.println("期待您的下次光临!");
    }

}

你可能感兴趣的:(Java,java)