Java基础案例2-3

【实验2-3】 超市购物小程序

编写一个超市购物程序,在一家超市有牙刷、毛巾、水杯、苹果和香蕉五种商品,商品价格如下表所示。
编号 商品名称 价格
1 牙刷 8.8元
2 毛巾 10.0元
3 水杯 18.8元
4 苹果 12.5元
5 香蕉 15.5元

用户输入商品序列号进行商品购买,用户输入购买数量后计算出所需要花费的钱,一次购买结束后,需要用户输入“Y”或“N”,“Y”代表继续购买,“N”代表购物结束。

package com.j2se.myInstances.example2_3;
import java.util.Scanner;
public class SuperMarketShopping {
    
    
    static double toothBrushPrice = 8.8;
    static double towelPrice = 10.0;
    static double cupPrice = 18.8;
    static double applePrice = 12.5;
    static double bananaPrice = 15.5;

    public static void main(String[] args) {
    
    
        meauInfo();
        String isFlag = "Y";
        while (isFlag.equals("Y")) {
    
    
            Scanner sc = new Scanner(System.in);
            int idx = sc.nextInt();
            System.out.println("您要购买该商品的数量:");
            switch (idx) {
    
    
                case 1:
                    int tonums = sc.nextInt();
                    double toexpense = tonums * toothBrushPrice;
                    System.out.println("您选择了牙刷" + tonums + "支,需支付" + toexpense + "元");
                    System.out.println("是否继续购买商品?是(输入Y)/否(输入N)");
                    isFlag = sc.next();
                    break;
                case 2:
                    int tnums = sc.nextInt();
                    double texpense = tnums * towelPrice;
                    System.out.println("您选择了毛巾" + tnums + "条,需支付" + texpense + "元");
                    System.out.println("是否继续购买商品?是(输入Y)/否(输入N)");
                    isFlag = sc.next();
                    break;
                case 3:
                    int cnums = sc.nextInt();
                    double cexpense = cnums * cupPrice;
                    System.out.println("您选择了水杯" + cnums + "个,需支付" + cexpense + "元");
                    System.out.println("是否继续购买商品?是(输入Y)/否(输入N)");
                    isFlag = sc.next();
                    break;
                case 4:
                    int anums = sc.nextInt();
                    double aexpense = anums * applePrice;
                    System.out.println("您选择了苹果" + anums + "kg,需支付" + aexpense + "元");
                    System.out.println("是否继续购买商品?是(输入Y)/否(输入N)");
                    isFlag = sc.next();
                    break;
                case 5:
                    int bnums = sc.nextInt();
                    double bexpense = bnums * bananaPrice;
                    System.out.println("您选择了苹果" + bnums + "kg,需支付" + bexpense + "元");
                    System.out.println("是否继续购买商品?是(输入Y)/否(输入N)");
                    isFlag = sc.next();
                    break;
                default:
                    System.out.println("无此商品,请重新输入!");
                    break;
            }
        }
        System.out.println("欢迎下次光临!");
    }

    public static void meauInfo() {
    
    
        System.out.println("----------超市商城-----------");
        System.out.println("1------牙刷¥" + toothBrushPrice + "/支----------");
        System.out.println("2------毛巾¥" + towelPrice + "/条---------");
        System.out.println("3------水杯¥" + cupPrice + "/个---------");
        System.out.println("4------苹果¥" + applePrice + "/kg---------");
        System.out.println("5------香蕉¥" + bananaPrice + "/kg---------");
        System.out.println("请选择需要购买的商品的编号:");
    }
}

Guess you like

Origin blog.csdn.net/qq_42899028/article/details/119452275