2、用数组保存每个月份的天数,输出每个月有多少天。
输入一个年份,输出该年份每个月的天数(提醒:闰年二月份多一天)
import java.util.Scanner;public class TianShu {public static void main(String[] args) {int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};Scanner s = new Scanner(System.in);System.out.println("请输入一个年份:");int year = s.nextInt();//判断闰年if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {a[1] += 1;//二月份天数加1}//输出每个月的天数for (int i = 0; i < a.length; i++) {System.out.println(year + "年第" + (i + 1) + "个月的天数:" + a[i]);}}
}