提示用户输入一个年份和一个3字母月份的字符串,显示该月的天数(区分平年和闰年)。

if语句:

import java.util.*;

public class month {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a year:");
        int year = input.nextInt();
        String mon = input.next();
        if (  (( year % 4==0) && (year % 100 != 0) ||(year % 400==0))&& (mon.equals("Feb")) ) {
            System.out.printf("%s %d has 29 days\n",mon,year);
        }else if (mon.equals("Jan")){
            System.out.printf("%s %d has 31 days",mon,year);
        }else if (mon.equals("Feb")){
            System.out.printf("%s %d has 28 days",mon,year);
        }else if (mon.equals("Mar")){
            System.out.printf("%s %d has 31 days",mon,year);
        }else if (mon.equals("Apr")){
            System.out.printf("%s %d has 30 days",mon,year);
        }else if (mon.equals("May")){
            System.out.printf("%s %d has 31 days",mon,year);
        }else if (mon.equals("Jun")){
            System.out.printf("%s %d has 30 days",mon,year);
        }else if (mon.equals("Jul")){
            System.out.printf("%s %d has 31 days",mon,year);
        }else if (mon.equals("Aug")){
            System.out.printf("%s %d has 31 days",mon,year);
        }else if (mon.equals("Sep")){
            System.out.printf("%s %d has 30 days",mon,year);
        }else if (mon.equals("Oct")){
            System.out.printf("%s %d has 31 days",mon,year);
        }else if (mon.equals("Nov")){
            System.out.printf("%s %d has 31 days",mon,year);
        }else if (mon.equals("Dec")){
            System.out.printf("%s %d has 31 days",mon,year);
        }else {
            System.out.printf("%s is not a correct month name",mon);
        }

    }
}

运行结果如下:

switch语句:(switch语句要简便很多 有没有)

import java.util.Scanner;

public class month_1 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a year:");
        int year = input.nextInt();
        String mon = input.next();
        if (  (( year % 4==0) && (year % 100 != 0) ||(year % 400==0))&& (mon.equals("Feb")) ) {
            System.out.printf("%s %d has 29 days\n",mon,year);
        }else {
            switch (mon){
                case "Jan":
                case "Mar":
                case "May":
                case "Jul":
                case "Aug":
                case "Oct":
                case "Dec": System.out.printf("%s %d has 31 days",mon,year);
                            break;
                case "Apr":
                case "Jun":
                case "Sep":
                case "Nov":System.out.printf("%s %d has 30 days",mon,year);
                           break;
                case "Feb": System.out.printf("%s %d has 28 days",mon,year);
                           break;
                default:System.out.printf("%s is not a correct month name",mon);
            }
        }
    }
}

 

 

### 回答1: 可以使用Python编程实现这个功能,具体代码如下: ```python year = int(input("请输入年份:")) month = int(input("请输入月份:")) if month in [1, 3, 5, 7, 8, 10, 12]: days = 31 elif month in [4, 6, 9, 11]: days = 30 else: if (year % 4 == and year % 100 != ) or year % 400 == : days = 29 else: days = 28 print(f"{year}年{month}有{days}天") ``` 首先,我们通过`input`函数获取用户输入年份月份,使用`int`函数将输入字符串转换为整数类型。 然后,我们使用`if-elif-else`语句判断这个月份有多少天。对于大月份(1、3、5、7、8、10、12),有31天;对于小月份(4、6、9、11),有30天;对于2月份,需要判断是否是闰年,如果是闰年则有29天,否则有28天。 最后,我们使用`print`函数输出结果,使用`f-string`格式字符串,将年份月份天数输出到屏幕上。 ### 回答2: 编程输入年份月份,输出这一年的这个月份有多少天,需要使用到一些基本的编程知识和算法,包括: 1. 判断闰年闰年有366天,非闰年有365天。判断方法是:① 能被4整除但不能被100整除的年份闰年;② 能被400整除的年份也是闰年。 2. 月份天数:不同的月份有不同的天数,需要根据输入月份和所属的年份进行判断。 下面是一个简单的Python示例代码,可以实现输入年份月份,输出所在年份的该天数的功能: ``` year = int(input("请输入年份:")) month = int(input("请输入月份:")) if ((year%4==0 and year%100!=0) or (year%400==0)): days_of_month = [31,29,31,30,31,30,31,31,30,31,30,31] # 闰年天数 else: days_of_month = [31,28,31,30,31,30,31,31,30,31,30,31] # 非闰年天数 print("该天数为:", days_of_month[month-1]) ``` 首先,我们从控制台输入年份月份,并将其转换为整数类型。然后,使用if语句判断该年份闰年还是非闰年,并根据结果初始化一个包含每个月份天数的列表。最后,从列表中取出对应月份天数,输出到控制台上。 ### 回答3: 编程实现输入年份月份,输出这一年的这个月份有多少天,可以使用以下算法: 1.首先判断输入年份是否为闰年闰年的条件是:能被4整除但不能被100整除,或者能被400整除。 2.根据月份判断这个月份有多少天。根据月份判断这个月份有多少天。根据平年闰年的情况可以分为两种情况: (1)平年的情况: 1、3、5、7、8、10、12有31天;4、6、9、11有30天;2有28天。 (2)闰年的情况: 1、3、5、7、8、10、12有31天;4、6、9、11有30天;2有29天。 3.输出计算得出的天数。 具体地,可以使用以下的python代码来实现: ```python year = int(input('请输入年份:')) month = int(input('请输入月份:')) # 判断是否为闰年 if (year%4==0 and year%100!=0) or year%400==0: leap_year = True print(year,'年是闰年') else: leap_year = False print(year,'年是平年') # 计算天数 if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12: days = 31 elif month == 4 or month == 6 or month == 9 or month == 11: days = 30 elif month == 2: if leap_year: days = 29 else: days = 28 print(year,'年',month,'月份有',days,'天') ``` 以上代码首先通过输入年份判断是否为闰年,并根据月份计算出天数,最后输出计算的结果,实现了输入年份月份,输出这一年的这个月份有多少天的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值