
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); } }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 2