简体   繁体   English

为什么我的方法不返回字符串或char到main?

[英]Why doesn't my method return a string or char to main?

I'm trying to complete a simple program using multiple methods to compute the grade of a test, yet my method will not return any letters. 我正在尝试使用多种方法来完成一个简单的程序,以计算测试成绩,但是我的方法不会返回任何字母。 The IDE says that my method must return a result of type String . IDE说我的方法必须返回String类型的结果。

public static String getGrade1(int num1) {
    if (num1 <= 100 && num1 >= 90) {
        String a = "A";
        return a;
    } else if (num1 < 90 && num1 >= 80) {
        String b = "B";
        return b;
    }else if (num1 < 80 && num1 >= 70) {
        String c = "C";
        return c;
    }else if (num1 < 70 && num1 >= 60) {
        String d = "D";
        return d;
    }else if (num1 < 60) {
        String f = "F";
        return f;
    }
}

Your functions must always return a value if it's not of the type void. 如果函数的类型不是void,那么它必须始终返回一个值。 The problem is that if you call getGrade1(110) your function wouldn't reach a return statement. 问题是,如果调用getGrade1(110)函数将无法返回return语句。 Add an else clause at the end (without a trailing if ) that returns something and it should stop giving you warnings. 在末尾添加else子句(不带if尾部),该子句返回某些内容,并且应停止向您发出警告。 This code should work: 此代码应工作:

public static String getGrade1(int num1) {
    if (num1 <= 100 && num1 >= 90) {
        return "A";
    } else if (num1 < 90 && num1 >= 80) {
        return "B";
    } else if (num1 < 80 && num1 >= 70) {
        return "C";
    } else if (num1 < 70 && num1 >= 60) {
        return "D";
    } else if (num1 < 60) {
        return "F";
    } else {
        return "";
    }
}

To fix your problem, it is easiest to just add a default return statement to the end of the method, for example: 要解决您的问题,最简单的方法是将默认的return语句添加到方法的末尾,例如:

public String method() {
    // Code
    return ""; // Return some default String value
}

Note: 注意:

If you would rather have an exception occur instead of returning a default value, you could do something like the following: 如果您希望发生异常而不是返回默认值,则可以执行以下操作:

public String method() throws Exception {
    // Code
    throw new Exception(); // Throw some exception
}

Try this: 尝试这个:

 public static String getGrade1(int num1) {
    String grade = "";
  if (num1 <= 100 && num1 >= 90) {
        grade = "A";
    } else if (num1 < 90 && num1 >= 80) {
        grade = "B";
    }else if (num1 < 80 && num1 >= 70) {
        grade = "C";
    }else if (num1 < 70 && num1 >= 60) {
        grade = "D";
    }else if (num1 < 60) {
        grade = "F";
    }else{
        grade = "NA";
    }
     return grade;
}

I believe if your return statement is in a if-else statement, then the compiler won't consider it as the return statement. 我相信,如果您的return语句在if-else语句中,则编译器不会将其视为return语句。 Do what vonNikalasson did, but get rid of the else statement at the end and just return at the end. 做vonNikalasson所做的事情,但是在结尾处摆脱else语句,然后在结尾处返回。

public static String getGrade1(int num1) {
    if (num1 <= 100 && num1 >= 90) {
        return "A";
    } else if (num1 < 90 && num1 >= 80) {
        return "B";
    } else if (num1 < 80 && num1 >= 70) {
        return "C";
    } else if (num1 < 70 && num1 >= 60) {
        return "D";
    } else if (num1 < 60) {
        return "F";
    }
    return;
}

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

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