一、从键盘分别输入年、月、日,判断这一天是当年的第几天。
注:判断一年是否是闰年的标准: 1)可以被4整除,但不可被100整除 或 2)可以被400整除
import java.util.Scanner;
public class lianxi2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份: "); int year = scanner.nextInt(); System.out.print("请输入月份: "); int month = scanner.nextInt(); System.out.print("请输入日期: "); int day = scanner.nextInt(); int days = 0; switch (month) { case 1: days = day; break; case 2: days = 31 + day; break; case 3: if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) { days = 31 + 29 + day; } else { days = 31 + 28 + day; } break; case 4: if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) { days = 31 + 29 + 31 + day; } else { days = 31 + 28 + 31 + day; } break; case 5: if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) { days = 31 + 29 + 31 + 30 + day; } else { days = 31 + 28 + 31 + 30 + day; } break; case 6: if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) { days = 31 + 29 + 31 + 30 + 31 + day; } else { days = 31 + 28 + 31 + 30 + 31 + day; } break; case 7: if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) { days = 31 + 29 + 31 + 30 + 31 + 30 + day; } else { days = 31 + 28 + 31 + 30 + 31 + 30 + day; } break; case 8: if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) { days = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day; } else { days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day; } break; case 9: if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) { days = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day; } else { days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day; } break; case 10: if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) { days = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; } else { days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; } break; case 11: if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) { days = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; } else { days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; } break; case 12: if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) { days = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; } else { days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; } break; default: System.out.println("输入的月份有误!"); break; } System.out.println(year + "年" + month + "月" + day + "日是当年的第" + days + "天"); scanner.close(); }
}
二、为一个给定的年份找出其对应的中国生肖。
中国的生肖基于12年一个周期, 每年用一个动物代表:rat、ox、tiger、rabbit、dragon、snake、horse、sheep、monkey、 rooster、dog、pig。 提示:2019年:猪 2019 % 12 == 3
import java.util.Scanner;
public class lianxi3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份: ");
int year = scanner.nextInt();
int remainder = year % 12; switch (remainder) { case 0: System.out.println(year + "年对应的生肖是: monkey"); break; case 1: System.out.println(year + "年对应的生肖是: rooster"); break; case 2: System.out.println(year + "年对应的生肖是: dog"); break; case 3: System.out.println(year + "年对应的生肖是: pig"); break; case 4: System.out.println(year + "年对应的生肖是: rat"); break; case 5: System.out.println(year + "年对应的生肖是: ox"); break; case 6: System.out.println(year + "年对应的生肖是: tiger"); break; case 7: System.out.println(year + "年对应的生肖是: rabbit"); break; case 8: System.out.println(year + "年对应的生肖是: dragon"); break; case 9: System.out.println(year + "年对应的生肖是: snake"); break; case 10: System.out.println(year + "年对应的生肖是: horse"); break; case 11: System.out.println(year + "年对应的生肖是: sheep"); break; } } }