Hello Guys! the question goes like this:
Create a while loop that will execute the following code 12 times using a counter that starts at 1 and is incremented by 1 each time through the loop.
Within the while loop, create a switch statement that will print out the number of days in each of the months of the year. Use fall-through capabilities so that you only have one print statement for all of the months that contain 31 days. Do not worry about leap years.
Print out the name of the month as well. You can copy and paste your month names array from the previous lab to help make this easier.
Here is my Answer:
int numOfMonth = 1;
String[] months ={"January", "February", "March", "April", "May", "June" , "July" , "August" , "September" , "October" , "November", "December"};
int[] daysOfMonths ={28, 30, 31};
// String output;
while(numOfMonth++ <= 12){
int index = numOfMonth -1;
switch(numOfMonth){
case 2:
System.out.println(months[index] + " has " + daysOfMonths[0] + " Days.");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(months[index] + " has " + daysOfMonths[1] + " Days.");
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(months[index] + " has " + daysOfMonths[2] + " Days.");
break;
default:
break;
}
}
This Code outputs All other values Correctly except that It DOESN't OUTPUT JANUARY
Please, WHAt SHall I do?