package com.cy.test;
import java.util.Scanner;
/**
* 题目:
* 输入某年某月某日,判断这一天是这一年的第几天?
* 注意:只允许使用int类型,不允许使用date类型
*/
public class daysTest {
public static void main(String[] args) {
int year;
int mouth;
int day = 0;
int days;
int d = 0;
int e = 0;
Scanner scanner = new Scanner(System.in);
do{
System.out.println("请输入年:");
year = scanner.nextInt();
System.out.println("请输入月:");
mouth = scanner.nextInt();
System.out.println("请输入日:");
days = scanner.nextInt();
if(mouth <0 || mouth > 12 || days < 0 || days >31){
System.out.println("输入有误!");
e = 1;
}
}while (e == 1);
for (int i = 0; i <mouth; i++) {
switch (i){
//大月
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:{
day = 31;
break;
}
//小月
case 4:
case 6:
case 9:
case 11:{
day = 30;
break;
}
//平月
case 2:{
//闰年:能被4整除但是不能被100整除、能被400整除
//闰年:29天 平年:28天
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
day = 29;
}else{
day = 28;
}
}
default:
break;
}
d = d + day;
}
System.out.println("这是" + year + "年的第" + (d+days) + "天");
}
}
java输入年月日,输出这是这一年的第几天
最新推荐文章于 2024-04-15 17:03:06 发布