麻辣小龙女 2021-09-12 12:56 采纳率: 66.7%
浏览 57
已结题

哪位友友帮我看看怎么写

img

  • 写回答

2条回答 默认 最新

  • 偷窃月亮的贼 2021-09-12 13:43
    关注

    记得把 Demo 改为你的类名:

    
    import java.util.Scanner;
    
    enum Goods {
    
        TOOTHBRUSH("牙刷", 1, 8.8),
        TOWEL("毛巾", 2, 10.0),
        WATER_CUP("水杯", 3, 18.8),
        APPLE("苹果", 4, 12.5),
        BANANA("香蕉", 5, 15.5);
    
        private final String goodName;
        private final int index;
        private final double price;
    
        Goods(String goodName, int index, double price) {
            this.goodName = goodName;
            this.index = index;
            this.price = price;
        }
    
        public static double getPrice(int index) {
            for (Goods good : Goods.values()) {
                if (good.index == index) {
                    return good.price;
                }
            }
            return 0;
        }
    }
    
    public class Demo {
    
        public static void main(String[] args) {
            double money = 0;
            Scanner scanner = new Scanner(System.in);
            while (true) {
                System.out.println("是否继续?输入”Y“继续购物,输入”N“退出:");
                if ("Y".equals(scanner.next())) {
                    double singlePrice;
                    System.out.print("请输入商品编号:");
                    singlePrice = Goods.getPrice(scanner.nextInt());
                    System.out.print("请输入购买数量:");
                    money += singlePrice * scanner.nextInt();
                    continue;
                }
                break;
            }
            System.out.println("总共消费:" + money);
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月20日
  • 已采纳回答 9月12日
  • 创建了问题 9月12日

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等