Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2012 , the program should display that February 2012 had 29 days. If the user entered month 3 and year 2015 , the program should display that March 2015 had 31 days.
Solution:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int month, year;
boolean isLeap;
System.out.print("Enter a number for the month (1 - Januray, 2 - February .... 12 - December):");
month = s.nextInt();
System.out.print("Enter a number for the year:");
year = s.nextInt();
// a leap year is divided by 4 without remainder, not divided by 100 or divided by 400
isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 100 == 0);
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if(month == 1) {
System.out.print("January " + year + " has 31 days.");
} else if(month == 3) {
System.out.print("March " + year + " has 31 days.");
} else if(month == 5) {
System.out.print("May " + year + " has 31 days.");
} else if(month == 7) {
System.out.print("July " + year + " has 31 days.");
} else if(month == 8) {
System.out.print("August " + year + " has 31 days.");
} else if(month == 10) {
System.out.print("October " + year + " has 31 days.");
} else if(month == 12) {
System.out.print("December " + year + " has 31 days.");
}
} else if(month == 4 || month == 6 || month == 9 || month == 11) {
if(month == 4) {
System.out.print("April " + year + " has 30 days.");
} else if(month == 6) {
System.out.print("June " + year + " has 30 days.");
} else if(month == 9) {
System.out.print("September " + year + " has 30 days.");
} else if(month == 11) {
System.out.print("November " + year + " has 30 days.");
}
} else {
if(isLeap) {
System.out.print("February " + year + " has 29 days.");
} else {
System.out.print("February " + year + " has 28 days.");
}
}
}
Help! I need to make an GUI that creates an order form for a clothing store.
ReplyDelete