【20171002】Java每日一练
程序员文章站
2022-07-12 08:56:32
...
【插播】已知年月日求星期(1900年之后)
代码实现1:
package c2;
import java.util.Scanner;
public class C2_03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int month;
int mmonth;
int year;
int date;
int day;
long total = 0;
System.out.println("请输入年份:");
Scanner sc3 = new Scanner(System.in);
year = sc3.nextInt();
System.out.println("请输入月份:");
Scanner sc = new Scanner(System.in);
month = sc.nextInt();
System.out.println("请输入日期:");
Scanner sc2 = new Scanner(System.in);
date = sc2.nextInt();
for(int j = 1900; j <= year ; j++)
{
if(((j % 100 != 0) && (j % 4 == 0)) || (j % 400) == 0){
total+=1;
}
}
total = total +(year - 1900) * 365 + date;
for(mmonth = month -1;mmonth >=1;mmonth --){
if(mmonth == 1)
{
total += 31;continue;
}
if(mmonth <= 2){
if(((year % 100 != 0) && (year % 4 == 0)) || (year % 400) == 0){
total +=29;continue;
}
else{
total += 28;continue;
}
}
if(mmonth <= 3)
{
total += 31;continue;
}
if(mmonth <= 4)
{
total += 30;continue;
}
if(mmonth <= 5)
{
total += 31;continue;
}
if(mmonth <= 6)
{
total += 30;continue;
}
if(mmonth <= 7)
{
total += 31;continue;
}
if(mmonth <= 8)
{
total += 31;continue;
}
if(mmonth <= 9)
{
total += 30;continue;
}
if(mmonth <= 10)
{
total += 31;continue;
}
if(mmonth <= 11 )
{
total += 30;continue;
}
}
System.out.println("这一天是第" + total + "天");
day = (int)(total % 7);
//System.out.println("今天是星期" + day);
switch(day)
{
case 1:
System.out.println("这一天是星期1。");
break;
case 2:
System.out.println("这一天是星期2。");
break;
case 3:
System.out.println("这一天是星期3。");
break;
case 4:
System.out.println("这一天是星期4。");
break;
case 5:
System.out.println("这一天是星期5。");
break;
case 6:
System.out.println("这一天是星期6。");
break;
default:
System.out.println("这一天是星期7。");
}
}
}
代码实现2:蔡勒公式
上一篇: 【20170926】Java每日一练
下一篇: Java每日一练(5)
推荐阅读
-
Java入门每日小练04
-
【每日一道算法题】Leetcode之longest-increasing-path-in-a-matrix矩阵中的最长递增路径问题 Java dfs+记忆化
-
整数反转Java版,每日一题系列(此题来自力扣网)
-
【JAVA】判断二分图——力扣每日一题(四)(2020.07.16)
-
739. 每日温度(Java)
-
【Java】力扣_每日一题_8.字符串转换整数(atoi)_中等
-
leetcode【每日一题】32. 最长有效括号 Java
-
leetcode【每日一题】109. 有序链表转换二叉搜索树 Java
-
寒假每日一题_AcWing1113.红与黑--Java代码
-
每日一练