记得把 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);
}
}
作业自己做,有问题提