输入日期,能根据输入的日期输出今天是星期几。
import java.util.Scanner;
public class Testcanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int nian = sc.nextInt();
System.out.println("请输入月份:");
int yue = sc.nextInt();
System.out.println("请输入多少号:");
int r = sc.nextInt();
int x = (nian - 1) / 4 - (nian - 1) / 100 + (nian - 1) / 400;//计算闰年数
int n = (nian - 1) * 365 + x;//计算从元年到输入年份这么多年的天数
int y = 0;
if ((nian % 4 == 0 && nian % 100 != 0) || nian % 400 == 0)//判断是不是闰年
{
if (yue == 1) {
y = 0;
} else if (yue == 2) {
y = 31;
} else if (yue == 3) {
y = 60;
} else if (yue == 4) {
y = 91;
} else if (yue == 5) {
y = 121;
} else if (yue == 6) {
y = 152;
} else if (yue == 7) {
y = 182;
} else if (yue == 8) {
y = 213;
} else if (yue == 9) {
y = 244;
} else if (yue == 10) {
y = 274;
} else if (yue == 11) {
y = 305;
} else if (yue == 12) {
y = 335;
}
} else {
if (yue == 1) {
y = 0;
} else if (yue == 2) {
y = 31;
} else if (yue == 3) {
y = 59;
} else if (yue == 4) {
y = 91 - 1;
} else if (yue == 5) {
y = 121 - 1;
} else if (yue == 6) {
y = 152 - 1;
} else if (yue == 7) {
y = 182 - 1;
} else if (yue == 8) {
y = 213 - 1;
} else if (yue == 9) {
y = 244 - 1;
} else if (yue == 10) {
y = 274 - 1;
} else if (yue == 11) {
y = 305 - 1;
} else if (yue == 12) {
y = 335 - 1;
}
}
int sum = n + y + r ;
int a = sum % 7;
switch (a) {
case 0:
System.out.println("今天是星期日");
break;
case 1:
System.out.println("今天是星期一");
break;
case 2:
System.out.println("今天是星期二");
break;
case 3:
System.out.println("今天是星期三");
break;
case 4:
System.out.println("今天是星期四");
break;
case 5:
System.out.println("今天是星期五");
break;
case 6:
System.out.println("今天是星期六");
break;
}
}
}
测试结果:
请输入年份:
2018
请输入月份:
7
请输入多少号:
17
今天是星期二
============
请输入年份:
2011
请输入月份:
4
请输入多少号:
25
今天是星期一
|
|