循环遍历java代码中的枚举值

时间:2016-01-31 05:28:28

标签: java

当我尝试通过常规for循环遍历枚举项时,我收到一些编译错误,是否有人可以帮助我解决问题?

bin

2 个答案:

答案 0 :(得分:1)

来自Java tutorial

for (weekDays day : weekDays.values()) {
    System.out.println("Enter the sales amount for " + day + ":");
    sales[day.ordinal()] = sc.nextDouble();
}

答案 1 :(得分:0)

以下代码适用于我的计算机。

public class WeeklySales
{
    public enum weekDays {Monday, Tuesday, Wednesday, Thursday, Friday};
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args)
    {
        double[] sales = new double[5];
        double total = 0;
        for (weekDays day : weekDays.values()) {
            System.out.println("Enter the sales amount for " + day + ":");
            sales[day.ordinal()] = sc.nextDouble();
            total += sales[day.ordinal()];
        }
        System.out.println("The total sales for Monday to Friday is: " + total);
    }
}