Java:键盘输入年、月、日,计算这一天是当年的第几天

1.思路分析

​ 1.1判断闰年或平年->确定2月天数(闰年29天,平年28天)->累加计算

​ 1.2将思路转换为数学模型

​ 闰年:能被4整除但不能被100整除,或能被400整数

​ 逐个月份累加即可

2.程序转换

​ 2.1将数学模型对应Java编程实现

​ 对应知识点:switch分支或for循环、if else分支、和运算符

​ 参考以下代码:

    //引入扫描器对象
    import java.util.Scanner;
    public class Switch2 {
        public static void main(String[] args) {
            //新建扫描器对象scan
            Scanner scan = new Scanner(System.in);
            //扫描3个整数表示年份、月份、日期
            System.out.println("请输入年份");
            int year = scan.nextInt();
            System.out.println("请输入月份");
            int month = scan.nextInt();
            System.out.println("请输入日期");
            int day = scan.nextInt();
            //判断闰年还是平年,确定2月天数
            int f;//2月天数
            int days=0;//天数
            if(year%4==0&&year%100!=0||year%400==0){
                f = 29;
            }else {
                f = 28;
            }
            switch (month){
                case 1:days = day;break;
                case 2:days = 31+day;break;
                case 3:days = 31+f+day;break;
                case 4:days = 31+f+31+day;break;
                case 5:days = 31+f+31+30+day;break;
                case 6:days = 31+f+31+30+31+day;break;
                case 7:days = 31+f+31+30+31+30+day;break;
                case 8:days = 31+f+31+30+31+30+31+day;break;
                case 9:days = 31+f+31+30+31+30+31+31+day;break;
                case 10:days = 31+f+31+30+31+30+31+31+30+day;break;
                case 11:days = 31+f+31+30+31+30+31+31+30+31+day;break;
                case 12:days = 31+f+31+30+31+30+31+31+30+31+30+day;break;
            }
            System.out.println(days);
        }
    }

2.2代码优化

​ 优化算法,让程序更简单,思路更明了,功能更强大

​ 参考:相对于程序1,可改变以下程序

switch (month){
    case 12:days += 30;
    case 11:days += 31;
    case 10:days += 30;
    case 9:days += 31;
    case 8:days += 31;
    case 7:days += 30;
    case 6:days += 31;
    case 5:days += 30;
    case 4:days += 31;
    case 3:days += f;
    case 2:days += 31;
    case 1:days += day;
}

2.3使用for循环实现

​ 相对于程序1,可将以下代码替换switch结构

for(int i =1;i<=month;i++){
    if(i==12||i==10||i==7||i==5){
        days+=30;
    }
    if(i==11||i==9||i==8||i==6||i==4||i==2){
        days+=31;
    }
    if(i==3){days+=f;}
    if(i==1){days+=day;}
}

Java中,你可以通过以下步骤编写一个简单的程序来判断给定日期是一中的第几天: 1. 首先,你需要获取用户的输入,包括。可以使用`Scanner`类来读取标准输入。 ```java import java.util.Scanner; public class DayOfYear { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入:"); int year = scanner.nextInt(); System.out.println("请输入(注意:01表示一):"); int month = scanner.nextInt(); System.out.println("请输入日期:"); int day = scanner.nextInt(); scanner.close(); } } ``` 2. 接着,你需要处理闰的规则,因为二有28天或29天。判断的条件是:能被4整除,但是不能被100整除,除非同时也能被400整除。 ```java boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } int daysInFebruary(int year) { return isLeapYear(year) ? 29 : 28; } ``` 3. 计算中前几个天数,并加上当前份的天数,得到全天数。 ```java int totalDays = 365; // 初到平的一般天数 if (isLeapYear(year)) { totalDays += 1; // 闰的二一天 } // 计算到当前份的天数 for (int i = 1; i < month; i++) { totalDays += getDaysInMonth(i, year); } // 加上当前份的天数 totalDays += day; System.out.println("该日期当年的第 " + totalDays + " 天."); ``` 4. 最后,为了完整性,我们还需要一个函数来计算每个天数。这个函数可以根据份和是否为闰来返回适当的值。 ```java private static int getDaysInMonth(int month, int year) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; default: return daysInFebruary(year); } } ``` 现在你的程序就能判断任意给定日期是一中的第几天了。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值