[案例2-3]超市购物小程序

 1.任务描述

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

编号

商品名称

价格

1

牙刷

8.8元

2

毛巾

10.0元

3

水杯

18.8元

4

苹果

12.5元

5

香蕉

15.5元

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

运行结果:

 代码如下:

import java.util.Scanner;

/**
 * 2022/1/24
 */
public class anli3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double toothbrush=8.8;   //牙刷价格
        double towel=10.0;        //毛巾价格
        double cup=18.8; 		    //水杯价格
        double apple=12.5;        //苹果价格
        double banana=15.5;       //香蕉价格

        String a = "Y";
        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 + "元");
        int ZongJianShu = 0;
        double i = 0;
        while (a.equals("Y")){
            System.out.println("请输入要购买商品的序号:");
            int id = in.nextInt();

            switch (id){
                case 1:
                    System.out.println("牙刷价格为:" +toothbrush+ "元,请输入要购买的数量:");
                    int s = in.nextInt();
                    double d = s * toothbrush;
                    System.out.println("您购买的牙刷数量为:" + s + "支,共消费:" + d + "元");
                    ZongJianShu += s;
                    i += d;
                    System.out.println("是否继续购买(Y,继续 N,退出):");
                    a = in.next();
                    break;
                case 2:
                    System.out.println("毛巾价格为:" + towel + "元,请输入要购买的数量:");
                    int s1 = in.nextInt();
                    double d1 = s1 * towel;
                    System.out.println("您购买毛巾数量为:" + s1 + "条,共消费:" + d1 + "元");
                    ZongJianShu += s1;
                    i += d1;
                    System.out.println("是否继续购买(Y,继续 N,退出):");
                    a = in.next();
                    break;
                case 3:
                    System.out.println("水杯价格为:" + cup + "元,请输入要购买的数量:");
                    int s2 = in.nextInt();
                    double d2 = s2 * cup;
                    System.out.println("您购买毛巾数量为:" + s2 + "条,共消费:" + d2 + "元");
                    ZongJianShu += s2;
                    i += d2;
                    System.out.println("是否继续购买(Y,继续 N,退出):");
                    a = in.next();
                    break;
                case 4:
                    System.out.println("苹果价格为:" + apple + "元,请输入要购买的数量:");
                    int s3 = in.nextInt();
                    double d3 = s3 * apple;
                    System.out.println("您购买毛巾数量为:" + s3 + "条,共消费:" + d3 + "元");
                    ZongJianShu += s3;
                    i += d3;
                    System.out.println("是否继续购买(Y,继续 N,退出):");
                    a = in.next();
                    break;
                case 5:
                    System.out.println("香蕉价格为:" + banana + "元,请输入要购买的数量:");
                    int s4 = in.nextInt();
                    double d4 = s4 * banana;
                    System.out.println("您购买毛巾数量为:" + s4 + "条,共消费:" + d4 + "元");
                    ZongJianShu += s4;
                    i += d4;
                    System.out.println("是否继续购买(Y,继续 N,退出):");
                    a = in.next();
                    break;
                default:
                    System.out.println("暂无该商品~~~~~~");
            }
        }
        System.out.println("一共买了:" + ZongJianShu + "件,共消费:" + i + "元");
        System.out.println("欢迎再次光临!");
    }
}

  • 19
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小陈努力学JAVA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值