第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;
 * 		}
 */
		
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章