package practicego;import java.util.scanner;/* * 2. Based on the month entered, determine how many days of the month */public class cto {public static void main (String[] args) {scanner sc = new scanner (system.in); System.out.println ("Enter month:"); Int month = sc.nextint ();switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:system.out.println (month+ " Month " + " has 31 days. "); Break;case 4:case 6:case 9:case 11:system.out.println (month+" month "+" has 30 days. Case 2:system.out.println ("Please enter the Year:"); Int year = sc.nextint ();//judgment is not a leap years if (year%4==0 && year%100!=0 | | year%400==0) {month = 29;} else {month = 28;} System.out.println ("February " + " with " +month+ "); Break;default:system.out.println (" Will you lose? You're not going to lose! "), Break;}}
Test:
Enter Month: November has 31 days.
Input Month: 2 Please enter year: 20,162 months with 29 days
Input Month: 2 Please enter year: 20,172 months with 28 days
Enter the month: 33 months with 31 days.
Java little exercise:. Determine how many days the month is based on the month entered