7-9 那天是周几呢? (10 分)
输入一个日期字符串例如2020-12-2,编程实现告诉我现在是星期几,然后是该年的第几天?
输入格式:
在一行中输入一个字符串日期例如2020-4-4
输出格式:
输出,在一行输出周几 另一行输出是该年的第几天
输入样例:
在这里给出一组输入。例如:
2020-11-17
结尾无空行
输出样例:
在这里给出相应的输出。例如:
2
322
结尾无空行
这里是我写的代码,仅供大家参考:
在这里插入代码片
```import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
int i=0;
int feb=0;
int day=0;
String s[]=new String[3];
for(String str1:str.split("-", 3))
{
s[i]=str1;
i++;
}
int a[]=new int[3];
for(int m=0;m<3;m++)
{
a[m]=Integer.parseInt(s[m]);
}
if(a[0]%4==0&&a[0]%100!=0&&a[0]%400!=0)
{
feb=29;
}
else
{
feb=28;
}
int month=a[1]-1;
while(month>0)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
day+=31;
month--;
}
else if(month==2)
{
day+=feb;
month--;
}
else
{
day+=30;
month--;
}
}
day=day+a[2];
System.out.println((day+2)%7);//一月一日是周三
System.out.println(day);
}
}