天天看点

第3章:给出一个月的总天数

/**
 * 给出一个月的总天数。
 * 提示用户输入月份和年份:
 * 显示这个月的天数。
 */
package Test;

import java.util.Scanner;

public class T311Scanner {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the month and year: ");
		int months = input.nextInt();
		int year = input.nextInt();
		
		int day = 0;
		boolean isLeapYear = 
				(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
//		System.out.println((isLeapYear == true) ? day == 29 :day == 28);
		if (isLeapYear == true)
			day = 29;
		else
			day = 28;
		
		if (months == 1 || months == 3 || months == 5 || months == 7
				|| months == 8 || months == 10 || months == 12)
			System.out.println("Jan. " + year + " has 31 days!");
		else
			System.out.println((months == 2) ? "Feb. " + year + " has " + day + " days!" : "Jan. " + year + " has 30 days!");

/**
 * 			System.out.println((months == 4 || months == 6 || months == 9 || months == 11)
 * 					 ? "Jan. " + year + " has 30 days!" : "Feb. " + year + " has " + day + " days!");
 * 		    
 * 		else if (months == 4 || months == 6 || months == 9 || months == 11)
 * 			System.out.println("Jan. " + year + " has 30 days!");
 * 		else
 * 			System.out.println("Feb. " + year + " has " + day + " days!");
 * 		
 * 		switch(months){
 * 		case 1: System.out.println("Jan. " + year + " has 31 days!");
 * 		        break;
 * 		case 2: System.out.println("Feb. " + year + " has " + day + " days!");
 *                break;
 * 		case 3: System.out.println("Mar. " + year + " has 31 days!");
 *                 break;
 * 		case 4: System.out.println("Apr. " + year + " has 30 days!");
 *                 break;
 * 		case 5: System.out.println("May. " + year + " has 31 days!");
 *                break;
 * 		case 6: System.out.println("Jun. " + year + " has 30 days!");
 *                 break;
 * 		case 7: System.out.println("Jul. " + year + " has 31 days!");
 *                 break;
 * 		case 8: System.out.println("Aug. " + year + " has 31 days!");
 *                 break;
 * 		case 9: System.out.println("Sep. " + year + " has 30 days!");
 *                break;
 * 		case 10: System.out.println("Oct. " + year + " has 31 days!");
 *                  break;
 * 		case 11: System.out.println("Nov. " + year + " has 30 days!");
 *                 break;
 * 		case 12: System.out.println("Dec. " + year + " has 31 days!");
 *                 break;
 * 		}
 */
		
	}
}