Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Chapter3_11NumberOfDaysInMonth {
- public static void main(String[] args) {
- Scanner kb = new Scanner(System.in);
- System.out.println("Enter a month and year. 1 for Jan, 2 for Feb, etc");
- // get user input
- int month = kb.nextInt();
- int year = kb.nextInt();
- int days = 0; // I will need to know how many days, so I set this variable initially to 0.
- // use the isLeapYear we wrote to determine if the year is a leap year
- boolean isLeapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0); // this will evalutate to true or fal
- if ( month == 1 ||month==3||month==5||month==7||month==8||month==10||month==12 ){
- days = 31;
- }
- else if (month==4||month==6||month==9||month==11){
- days = 30;
- }
- else if (month == 2){
- if (isLeapYear)
- days = 29;
- else
- days=28;
- }
- // use a switch statement to get the name of the month. It's a cleaner if, else if, way to go about it.
- String name="";
- switch (month){
- case 1: name = "January";break;
- case 2: name = "February";break;
- case 3: name = "March";break;
- case 4: name = "April";break;
- case 5: name = "May";break;
- case 6: name = "June";break;
- case 7: name = "July";break;
- case 8: name = "August";break;
- case 9: name = "September";break;
- case 10: name = "October";break;
- case 11: name = "November";break;
- case 12: name = "December";break;
- }
- System.out.println( name + " "+ year + " has "+ days + " days.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment