Java判断某天是这一年的第几天
输入某年某月某日,判断这一天是这一年的第几天?
方法一:
package test;
import java.util.Scanner;
public class Day {
public static void main(String[] args) {
int i,t=0,s=0;
Scanner sc=new Scanner(System.in);
int y=sc.nextInt();
int m=sc.nextInt();
int d=sc.nextInt();
for(i=1;i<m;i++)
{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
t=31;break;
}
case 4:
case 6:
case 9:
case 11:
{
t=30;break;
}
case 2:
{
if(y%4==0&&y%100!=0||y%400==0)
{
t=29;
}
else t=28;
break;
}
default:break;
}
s=s+t;
}
System.out.println(s+d);
sc.close();
}
}
方法二:
package test;
import java.util.Scanner;
public class Shishi {
public static void main(String[] args) {
int i,s=0;
int[] arr={31,28,31,30,31,30,31,31,30,31,30,31};
Scanner sc=new Scanner(System.in);
int y=sc.nextInt();
int m=sc.nextInt();
int d=sc.nextInt();
if(y%4==0&&y%100!=0||y%400==0)
{
arr[1]=29;
}
for(i=0;i<m-1;i++)
{
s=s+arr[i];
}
System.out.println(s+d);
}
}