java习题 判断某一天是一年的第几天
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
package src;来自百度文库
import java.util.*;
public class year {
public static void main(String args[]) {
for (; true;) {
boolean b = false;
int sum = 0, days = 0;
@SuppressWarnings("resource")
int month = sca.nextInt();
if (month < 0 || month > 12) {
System.out.println("输入月份错误");
break;
}
System.out.println("请输入日期");
int day = sca.nextInt();
if (day < 0 || day > 31) {
Scanner sca = new Scanner(System.in);
System.out.println("请输入年份");
int year = sca.nextInt();
if (year <= 0) {
System.out.println("输入年份错误");
break;
}
System.out.println("请输入月份");
} else {
System.out.println("这天是第" + (sum + day) + "天");
}
}
}
}
|| ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)) {
System.out.println("日期错误");
break;
} else if (month == 2 && day > 28) {
System.out.println("日期错误");
break;
}
//判断闰年和小月的日期
for (int i = 1; i < month; i++) { //计算天数
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if (b) {
days = 29;
} else {
days = 28;
}
break;
}// end switch
sum += days;
} // end for
if (b) {
System.out.println("这天是第" + (sum + day) + "天");
System.out.println("输入日期错误");
break;
}
//首先判断是不是闰年
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
b = true;
}
if ((b && month == 2 && day > 29)
import java.util.*;
public class year {
public static void main(String args[]) {
for (; true;) {
boolean b = false;
int sum = 0, days = 0;
@SuppressWarnings("resource")
int month = sca.nextInt();
if (month < 0 || month > 12) {
System.out.println("输入月份错误");
break;
}
System.out.println("请输入日期");
int day = sca.nextInt();
if (day < 0 || day > 31) {
Scanner sca = new Scanner(System.in);
System.out.println("请输入年份");
int year = sca.nextInt();
if (year <= 0) {
System.out.println("输入年份错误");
break;
}
System.out.println("请输入月份");
} else {
System.out.println("这天是第" + (sum + day) + "天");
}
}
}
}
|| ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)) {
System.out.println("日期错误");
break;
} else if (month == 2 && day > 28) {
System.out.println("日期错误");
break;
}
//判断闰年和小月的日期
for (int i = 1; i < month; i++) { //计算天数
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if (b) {
days = 29;
} else {
days = 28;
}
break;
}// end switch
sum += days;
} // end for
if (b) {
System.out.println("这天是第" + (sum + day) + "天");
System.out.println("输入日期错误");
break;
}
//首先判断是不是闰年
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
b = true;
}
if ((b && month == 2 && day > 29)