Creating a input method that prints right amount of days in a month/Int can't be converted into string

huangapple go评论142阅读模式
英文:

Creating a input method that prints right amount of days in a month/Int can't be converted into string

问题

import java.util.*;

public class findDays {
    public static int monthDays(int month) {
        Scanner key = new Scanner(System.in);
        System.out.println("请输入月份:");
        month = key.nextInt();

        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            return 31;
        } else if (month == 2) {
            return 28;
        } else {
            return 30;
        }
    }
}
英文:

For my code I am supposed to set a method that will ask for the month for parameters then print out the right amount of days that month had.

I have developed if statements for each day of the month for when the user inputs a month, however, I keep getting a message saying int cannot be converted into string.

I was told I was suppose to return the number of days

[Example January should return 31]

however I am not exactly sure how to do this. Instead of trying to print "31" do I put in "return 31"?

import java.util.*;

public class findDays{
    public static String monthDays(int month){
        Scanner key = new Scanner(System.in);
        System.out.println("Type a month");
        month = key.nextInt();

        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) 
        {
            System.out.println("31");
        }
        else if(month == 2)
        {
            System.out.println("28");
        }
        else
        {
            System.out.println("30");
        }
   }
}

答案1

得分: 1

以下是翻译好的内容:

不需要传递参数,使用 java.time.* 库可以大大帮助。

以下是一个建议,作为上述代码的替代,还可以指定方法是否返回闰年中某个月的天数,这也可以增强功能。二月份的天数在闰年和平年时可能不同:

public static String monthDays(boolean isLeapYear) {
    int month;
    Scanner key = new Scanner(System.in);
    System.out.println("输入一个月份(1到12之间的数字)");
    month = key.nextInt();
    if (month > 12 || month < 1)
        throw new IllegalArgumentException("月份无效。");
    int thisYear = LocalDate.now().getYear();
    YearMonth yearMonth;
    int year = 0;
    if (isLeapYear) {
        if (Year.of(thisYear).isLeap())
            year = thisYear;
        else {
            for (int i = 1; i < 4; i++)
                if (Year.of(thisYear + i).isLeap())
                    year = thisYear + i;
        }
    } else {
        if (Year.of(thisYear).isLeap())
            year = thisYear + 1;
        else
            year = thisYear;
    }
    yearMonth = YearMonth.of(year, month);
    System.out.println("插入的月份:" + yearMonth.getMonth());
    int monthDays = yearMonth.lengthOfMonth();
    System.out.println("月份的天数:" + monthDays);
    return String.valueOf(monthDays);
}

值得注意的是,在您的方法和建议的方法中,都需要确定年份,以便更准确地计算一个月的天数。

首先,我们可以将年份设置为当前年份,然后根据闰年标志以及今年是否为闰年,我们将找出插入月份的天数。

希望对您有所帮助。

英文:

There would be no need to pass the argument, and using java.time.* library could help substantially.

Here is a suggestion as a replacement of the code above, specifying whether does the method return a number of days of a month in a leap year or not could enhance the functionality as well.
The number of days in Month of February could be different if leap year:

public static String monthDays(boolean isLeapYear) {
    int month;
    Scanner key = new Scanner(System.in);
    System.out.println(&quot;Type a month (a number in range of 1 to 12)&quot;);
    month = key.nextInt();
    if (month &gt; 12 || month &lt; 1)
        throw new IllegalArgumentException(&quot;The month number is invalid.&quot;);
    int thisYear = LocalDate.now().getYear();
    YearMonth yearMonth;
    int year = 0;
    if (isLeapYear) {
        if (Year.of(thisYear).isLeap())
            year = thisYear;
        else {
            for (int i = 1; i &lt; 4; i++)
                if (Year.of(thisYear + i).isLeap())
                    year = thisYear + i;
        }
    } else {
        if (Year.of(thisYear).isLeap())
            year = thisYear + 1;
        else
            year = thisYear;
    }
    yearMonth = YearMonth.of(year, month);
    System.out.println(&quot;Inserted month: &quot; + yearMonth.getMonth());
    int monthDays = yearMonth.lengthOfMonth();
    System.out.println(&quot;Number of days in month: &quot; + monthDays);
    return String.valueOf(monthDays);
}

This is noteworthy that both in your method and the suggested one, it is required to determine the year so that calculating the number of days in a month would be more accurate.

To start, we could set the year as currentYear and based on the leapYear flag and whether this year is a leap year or not, we will find out the number of days in the inserted month.

Hope that helps out.

答案2

得分: 0

要从方法中返回一个值,你将使用return关键字,后面跟着你想要返回的值。在你的情况下,你只是打印了每个月的值,如果你想要返回它,将它改成:

if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) 
{
    System.out.println("31");
    return "31";
}

这将既打印该值,又返回它。

另外,你可以实现一个switch case语句,它在像这样的情况下比if语句更快速和更干净:

switch(month) {
    case 1:
    case 3:
    case 5:
    //.... 所有31天的月份在这里
    return 31;
    
    case 2:
    return 28;
    default: 
    return 30;
}

default关键字意味着如果值不匹配任何case子句,将执行default,类似于你代码中的else

这篇文章更详细地解释了switch语句,如果你想要了解更多,可以参考这里

英文:

To return a value from a method, you'll use the return keyword followed by whichever value you want to return, in your case, you are just printing the value for each month, if you want to return it, change it to

if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) 
        {
            System.out.println(&quot;31&quot;);
            return &quot;31&quot;;
        }

This will both print the value, and then return it.
Also you could implement a switch case, it is faster and cleaner than the if statement in cases like that:

switch(month) {
    case 1:
    case 3:
    case 5:
    //.... all your 31 day months here
    return 31;
    
    case 2:
    return 28;
    default: 
    return 30;
}

The default keyword means that, if the value does not match any of the case clauses, the default will be executed, similar to the else in your code.

This article explains the switch statement in more detail if you wanna read more about it.

huangapple
  • 本文由 发表于 2020年5月4日 06:07:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61582279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定