import java.util.Scanner;
/*
* 给定一个日期,输出这个日期是该年的第几天。*/
public class HDU_oj2005 {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
// 数组初始化 每个月的天数
int m[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
while (sn.hasNext()) {
String s = sn.nextLine(); // 定义一个字符串变量并输入它
String[] str = s.split("/"); // 把我们输入的字符串用/分开并存储在str数组中
int yy = Integer.parseInt(str[0]);
int mm = Integer.parseInt(str[1]);
int dd = Integer.parseInt(str[2]);
int flag = 0; // 表示闰年的标记
// 是闰年
if (yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0) {
flag = 1;
}
int ans = 0; // 定义统计天数的变量
for (int i = 0; i < mm - 1; i++) {
ans = ans + m[i];
}
ans = ans + dd;
if (flag == 1 && mm > 2) {
ans++;
}
System.out.println(ans);
}
}
}
记得提交的时候类名写 Main
计算日期是年度第几天
本文介绍了一个Java程序,用于计算给定日期是该年的第几天。程序接收一个日期输入,格式为“年/月/日”,并考虑到闰年的影响。通过数组存储每月天数,判断是否为闰年,然后累加天数。
757

被折叠的 条评论
为什么被折叠?



