一个查询任意年份中任意月份的天数

我的作答:

package day20180917;
import java.util.Scanner;
public class ZuoYe02Modified {

public static void main(String[] args) {
// 定义年份
int year = -1;
// 定义月份
int month = -1;

// 使用Scanner
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年(例如2012)");
year = scanner.nextInt();
System.out.println("请输入月(例如2)");
month = scanner.nextInt();

System.out.println(year + "年" + month + "月有" + days(year, month) + "天");

}

public static int days(int year, int month) {
int days = 0;
if (month != 2) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;

}
} else {
//闰年
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
days = 29;
else
days = 28;

}
return days;

}

}

截图仍如图。

猜你喜欢

转载自www.cnblogs.com/SUN99bk/p/9663122.html