我不能在java中返回字符串值

2g32fytz  于 2021-07-03  发布在  Java
关注(0)|答案(4)|浏览(257)

我的作业需要我为输入的双精度值返回一个字符串值。我试过几件事,但都没用。可能这对你来说太基本了,但我在这里或网上找不到答案,可能是因为我不知道该去哪里找。如果你能指出去哪里找我或者帮我,那就太好了。非常感谢你!首先我试了一下:

public static String gradeLetter(double grade) {
    if (grade<=34) {
        String c = "FF";
        return c;
    }
    if (grade<=45 && grade >= 35) {
        String c = "FD";
        return c;
    }
    if (grade<=52 && grade >= 46) {
        String c = "DD";
        return c;
    }
    if (grade<=59 && grade >= 53) {
        String c = "DC";
        return c;
    }
    if (grade<=66 && grade >= 60) {
        String c = "CC";
        return c;
    }
    if (grade<=73 && grade >= 67) {
        String c = "CB";
        return c;
    }
    if (grade<=80 && grade >= 74) {
        String c = "BB";
        return c;
    }
    if (grade<=87 && grade >= 81) {
        String c = "BA";
        return c;
    }
    if (grade<=100 && grade >= 88) {
        String c = "AA";
        return c;
    }
}

但没用所以我试了这个:

public static String gradeLetter(double grade) {
    String c = "";
    if (grade<=34) {
        String a = "FF";
    }
    if (grade<=45 && grade >= 35) {
        String a = "FD";
    }
    if (grade<=52 && grade >= 46) {
        String a = "DD";
    }
    if (grade<=59 && grade >= 53) {
        String a = "DC";
    }
    if (grade<=66 && grade >= 60) {
        String a = "CC";
    }
    if (grade<=73 && grade >= 67) {
        String a = "CB";
    }
    if (grade<=80 && grade >= 74) {
        String a = "BB";
    }
    if (grade<=87 && grade >= 81) {
        String a = "BA";
    }
    if (grade<=100 && grade >= 88) {
        String a = "AA";
    }
    return c+a;
}

还是不走运。。。两者都给出了编译错误。

pgky5nke

pgky5nke1#

您在函数中使用了一个局部变量,因此它返回null。您可以尝试:

public static String gradeLetter(double grade) {
    String a = "";
    if (grade<=34) {
         a = "FF";
    }
    if (grade<=45 && grade >= 35) {
         a = "FD";
    }
    if (grade<=52 && grade >= 46) {
         a = "DD";
    }
    if (grade<=59 && grade >= 53) {
         a = "DC";
    }
    if (grade<=66 && grade >= 60) {
         a = "CC";
    }
    if (grade<=73 && grade >= 67) {
         a = "CB";
    }
    if (grade<=80 && grade >= 74) {
         a = "BB";
    }
    if (grade<=87 && grade >= 81) {
         a = "BA";
    }
    if (grade<=100 && grade >= 88) {
         a = "AA";
    }
    return a;
}

如果没用就告诉我

ktecyv1j

ktecyv1j2#

在您的第一个例子中,我发现您得到了一个compile erro,因为您错过了方法末尾的一个返回(在if语句之外)

public static String gradeLetter(double grade) {
    if (grade<=34) {

...
    if (grade<=100 && grade >= 88) {
        String c = "AA";
        return c;
    }
   return null;
}

在第二种情况下,您尝试访问 a 超出定义范围:

public static String gradeLetter(double grade) {
    String a = "";
    if (grade<=34) {
        a = "FF";
    }
    if (grade<=45 && grade >= 35) {
        a = "FD";
    }
    ....
    return a;
}
2izufjch

2izufjch3#

这里有两个主要问题:1)变量a在grade>100的情况下没有声明,而program在这种情况下会抛出错误2)没有必要检查所有这些条件,因为只有一个条件是真的,所以最好使用if-else-if语句,如:

public static String gradeLetter(double grade) {
        String a ="";
        if (grade<=34) {
            a = "FF";
        }else if (grade<=45 && grade >= 35) {
            a = "FD";
        }else if (grade<=52 && grade >= 46) {
            a = "DD";
        }else if (grade<=59 && grade >= 53) {
            a = "DC";
        }else if (grade<=66 && grade >= 60) {
            a = "CC";
        }else if (grade<=73 && grade >= 67) {
            a = "CB";
        }else if (grade<=80 && grade >= 74) {
             a = "BB";
        }else if (grade<=87 && grade >= 81) {
            a = "BA";
        }else if (grade<=100 && grade >= 88) {
            a = "AA";
        }
        return a;
 }

通过这种方式,您将确保您的方法将返回一个值(即使这是一个空字符串),并且您将不会花时间检查将是假的语句

s6fujrry

s6fujrry4#

试试这个

public class HelloWorld {
    public static String gradeLetter(double grade) {
        String stringValue = "";

        if (grade <= 45 && grade >= 35) {
            stringValue = "FD";
        } else if (grade <= 52 && grade >= 46) {
            stringValue = "DD";
        } else if (grade <= 59 && grade >= 53) {
            stringValue = "DC";
        } else if (grade <= 66 && grade >= 60) {
            stringValue = "CC";
        } else if (grade <= 73 && grade >= 67) {
            stringValue = "CB";
        } else if (grade <= 80 && grade >= 74) {
            stringValue = "BB";
        } else if (grade <= 87 && grade >= 81) {
            stringValue = "BA";
        } else if (grade <= 100 && grade >= 88) {
            stringValue = "AA";
        } else {
            stringValue =  "FF";
        }

        return stringValue;
    }

    public static void main(String [] args) {
        System.out.println(gradeLetter(75.59));
    }
}

相关问题