I receive a few compiling errors when I try to loop through an enum items by a regular for loop, could anyone help me to solve the problem?
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;
weekDays day;
for(day = weekDays.Monday; day <= weekDays.Friday; day = (weekDays)(day + 1))
{
System.out.println("Enter the sales amount for " + day + ":");
sales[day] = sc.nextDouble();
}
for(day = weekDays.Monday; day <= weekDays.Friday; day = (weekDays)(day +1))
total += sales[day];
System.out.println("The total sales for Monday to Friday is: " + total);
}
}
for(day = weekDays.Monday; day <= weekDays.Friday; day = (weekDays)(day + 1))... can you explain why you think that this should work? Have you tried to search for working ways to iterate over an enum?sales[day]can you explain why you think that an enum value would be a suitable index for an array?ordinal()method.