繁体   English   中英

如何在 INT 方法中不返回值?

[英]How to NOT return value in INT method?

当我运行代码时,它返回:

31 和 -1

我怎样才能摆脱-1? 有没有办法在方法 INT 中不返回? 我试图返回 java.lang.Integer(null) 但它给了我一个错误。 我想我以错误的方式使用它。

这是代码:

package com.company;

public class Main {

public static void main(String[] args) {
    int y = getDaysInMonth(1, 2020);
    System.out.println(y);
}

public static boolean isLeapYear(int year) {
    if (year > 1 && year < 9999) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true;
        }
        return false;
    }
    return false;
}

public static int getDaysInMonth(int month, int year) {
    if ((month < 1 || month > 12) && (year < 1 || year > 9999)) {
        return -1;
    } else if (!isLeapYear(year)) {
        switch (month) {
            case 1:
                System.out.println(31);
                break;
            case 2:
                System.out.println(28);
                break;
            case 3:
                System.out.println(31);
                break;
            case 4:
                System.out.println(30);
                break;
            case 5:
                System.out.println(31);
                break;
            case 6:
                System.out.println(30);
                break;
            case 7:
                System.out.println(31);
                break;
            case 8:
                System.out.println(31);
                break;
            case 9:
                System.out.println(30);
                break;
            case 10:
                System.out.println(31);
                break;
            case 11:
                System.out.println(30);
                break;
            case 12:
                System.out.println(31);
                break;
            default:
                return -1;
        }
    } else if (isLeapYear(year)) {
        switch (month) {
            case 1:
                System.out.println(31);
                break;
            case 2:
                System.out.println(29);
                break;
            case 3:
                System.out.println(31);
                break;
            case 4:
                System.out.println(30);
                break;
            case 5:
                System.out.println(31);
                break;
            case 6:
                System.out.println(30);
                break;
            case 7:
                System.out.println(31);
                break;
            case 8:
                System.out.println(31);
                break;
            case 9:
                System.out.println(30);
                break;
            case 10:
                System.out.println(31);
                break;
            case 11:
                System.out.println(30);
                break;
            case 12:
                System.out.println(31);
                break;
            default:
                return -1;
        }
    } else return -1;
    return -1;
}

}

我几乎尝试了所有应该有的东西我还不知道。

这是因为您首先在方法中打印,然后在 y 中再次打印方法的返回值。

不要每次都打印,而是尝试返回值。 此外,您编写的程序可以非常有效地编写。 这是一个例子:

public static int getDays(int monthNumber, int yearNumber)
{

if (monthNumber == 2 && !isLeapYear(yearNumber))
    return 28;
else if (monthNumber==2)
    return 29;
else if ((monthNumber >= 1) && (monthNumber <= 7) && (monthNumber % 2 ==1))
    return 31;
else if ((monthNumber >= 8) && (monthNumber %2==0))
    return 31;
else 
    return 30;
}

有没有办法在方法 INT 中不返回?

是的,当然。 抛出异常。 例如:

if (month < 1 || month > 12) {
    throw new IllegalArgumentException("month value is out of range");
} 

(提示:我注意到您现有错误检查中的一个错误。重写时仔细查看。)

抛出异常会导致方法终止而不返回任何结果。 即使方法签名说应该返回结果。 您可以在 Oracle Java 教程中阅读它: https : //docs.oracle.com/javase/tutorial/essential/exceptions/definition.html Java 教程的那部分解释了什么是异常,如何以及何时抛出它们,如何捕捉它们,以及当你没有捕捉到它们时会发生什么。

话虽如此,有几个地方您要返回-1 您需要仔细检查每一项以决定是否真的可行。 如果getDaysInMonth的输入有效,那么您应该始终能够计算“一个月中的天数”值。 而且您应该只需要在一处检查参数。

我的建议是在方法开始时检查参数值。 然后可以根据参数有效的假设对方法的其余部分进行编码。


最后,如果getDaysInMonthgetDaysInMonth(1, 2020)返回-1 ,则表明您在该方法的逻辑中存在错误。 我建议您使用调试器来找到它……如果您无法通过阅读和逻辑分析您的代码来发现它。

仔细查看你的函数,就像现在一样,它总是返回 -1 而没有别的。

函数声明public static int getDaysInMonth(int month, int year)必须返回一个int值,如果它成功完成

它可能退出而不返回值的唯一情况是抛出异常。

因此,我将假设您的函数需要返回该月的天数,而不仅仅是打印它。

这是永远不会返回 -1 或除 28、29、30、31 以外的任何值的代码:

public static int getDaysInMonth(int month, int year) throws IllegalArgumentException {
    if (year < 1 || year > 9999) {
        throw new IllegalArgumentException(String.format("Invalid year %d must be between 1 and 9999", year));
    }
   
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:            
            System.out.println(31);
            return 31;

        case 2:
            if (isLeapYear(year)) {
                System.out.println(29);
                return 29;
            } else {
                System.out.println(28);
                return 28;
            }

        case 4:
        case 6:
        case 9:
        case 11:
            System.out.println(30);
            return 30;
    }

    throw new IllegalArgumentException(String.format("Invalid month %d must be between 1 and 12", month));
}

请注意以下更改:

  • Java 中的case是“fall through”,这意味着您不必一遍又一遍地编写相同的代码,您可以将具有相同结果的 case 放在另一个 case 下,而无需使用brake语句,它们将一起工作以提供相同的结果.

  • 在函数开始时,我们只检查年份是否有效。 如果月份无效,则不执行case语句,代码将直接转到switch块之后的行。 此时我们知道月份无效,因此无需检查,只需抛出异常即可。

  • 任何抛出异常的函数都必须在函数头中声明它这样做,方法是添加throws子句并列出用逗号分隔的异常。

  • 当你调用一个抛出异常的函数时,你必须将它包装在try .. catrch block 中

Java 方法只能返回一种类型的结果: https : //www.javatpoint.com/method-in-java

返回类型:返回类型是方法返回的数据类型。 它可能具有原始数据类型、对象、集合、void 等。如果该方法不返回任何内容,则使用 void 关键字。

作为解决方法:

使用包装类

您可以使用Integer类作为返回类型并返回 null。 它并不比 -1 好多少,但它表明没有返回任何内容。

无效参数异常

抛出这个: https : //docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html这是它的用途

default:
     throw new IllegalArgumentException("Invalid month value - out of range");

创建枚举

创建一个枚举类型并将其作为方法中的参数。 这样就不会有任何方法获得未知值。

public enum Month {
    January(1), February(2), March(3), April(4),May(5),June(6), July(7), August(8), September(9), October(10),  November(11), December(12)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM