猿问

为什么我没有得到任何值?

运行以下方法时,我没有得到任何返回。总是被终止而没有任何结果。有人可以告诉我为什么我没有得到任何结果吗?


我根据评论进行了调整,但没有成功。我在下面添加了主要方法;


public class ModuleGrader {

final int examID = 123;

String excellent =null;

String good=null;

String satisfactory=null;

String compensatableFail=null;

String outrightFail=null;

int grade;


public String gradeModule(int mark) {

    String result = null;

    if (mark>=70 && mark<=100) 

    {

        result = excellent;

        System.out.println(" ");

        }

    else if (mark>=60 && mark<=69)

    {

        result = good;

    }

    else if (mark>=50 && mark<=59)

    {

        result = satisfactory;

    }

    else if (mark>=40 && mark<=49)

    {

        result = compensatableFail;

    }

    else if (mark>=0 && mark<=39) {

        result = outrightFail;

    }

    else {

        System.out.println("Invalid entery, please insert an number between 100-0");

                }


    return result;

}

所以我添加了我的调用 main 方法;


调用的方法可能是问题所在?


public static void main(String[] args) {

        ModuleGrader mg=new ModuleGrader();

        mg.gradeModule(100);

        mg.gradeModule(66);}


喵喔喔
浏览 96回答 2
2回答

狐的传说

String excellent;您没有为,分配任何值String good;,因此它会失败,因为在调用它们时这些值尚未初始化为任何值。你怎么知道它不起作用?您没有将最终结果输出到控制台。我添加了System.out.println()纠正这一点。你不能从静态的东西引用非静态的东西。更改public class ModuleGrader为public static class ModuleGrader.最终工作代码public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; ModuleGrader mg=new ModuleGrader();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mg.gradeModule(100));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mg.gradeModule(66));&nbsp; &nbsp; }&nbsp; &nbsp; public static class ModuleGrader {&nbsp; &nbsp; &nbsp; &nbsp; final int examID = 123;&nbsp; &nbsp; &nbsp; &nbsp; String excellent = null;&nbsp; &nbsp; &nbsp; &nbsp; String good = null;&nbsp; &nbsp; &nbsp; &nbsp; String satisfactory = null;&nbsp; &nbsp; &nbsp; &nbsp; String compensatableFail = null;&nbsp; &nbsp; &nbsp; &nbsp; String outrightFail = null;&nbsp; &nbsp; &nbsp; &nbsp; int grade;&nbsp; &nbsp; &nbsp; &nbsp; public String gradeModule(int mark) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String result = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mark >= 70 && mark <= 100) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = excellent;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (mark >= 60 && mark <= 69) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = good;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (mark >= 50 && mark <= 59) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = satisfactory;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (mark >= 40 && mark <= 49) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = compensatableFail;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (mark >= 0 && mark <= 39) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = outrightFail;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid entery, please insert an number between 100-0");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

RISEBY

public class ModuleGrader {&nbsp; &nbsp; final int examID = 123;&nbsp; &nbsp; //String excellent=null;&nbsp; &nbsp; //String good=null;&nbsp; &nbsp; //String satisfactory=null;&nbsp; &nbsp; //String compensatableFail=null;&nbsp; &nbsp; //String outrightFail=null;&nbsp; &nbsp; int grade;&nbsp; &nbsp; public String gradeModule(int mark) {&nbsp; &nbsp; &nbsp; &nbsp; String result = null;&nbsp; &nbsp; &nbsp; &nbsp; if (mark>=70 && mark<=100)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = "excellent";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (mark>=60 && mark<=69)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = "good";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (mark>=50 && mark<=59)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = "satisfactory";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (mark>=40 && mark<=49)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = "compensatableFail";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (mark>=0 && mark<=39) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = "outrightFail";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid entery, please insert an number between 100-0");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答