为什么我没有收回任何价值?

时间:2019-09-12 22:24:59

标签: java return

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

我已经根据评论进行了调整,但是还没有工作。我在下面添加了主要方法;

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;
}

因此,我添加了调用主方法;

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

public static void main(String[] args) {
        ModuleGrader mg=new ModuleGrader();
        mg.gradeModule(100);
        mg.gradeModule(66);}

2 个答案:

答案 0 :(得分:1)

您没有为 confirmSuccess = () => { this.setState(() => ({ success: true })); this.props.history.push("/login", { fromRegistration: true }); }; String excellent;分配任何值,因此失败,因为在调用它们时这些值尚未初始化为任何值。

  1. 您怎么知道它不起作用?您没有最终结果输出到控制台。我添加了String good;来更正此问题。

  2. 您不能从静态对象中引用非静态对象。将System.out.println()更改为public class ModuleGrader

最终工作代码

public static class ModuleGrader

您需要在要分配的字符串两边加上引号。

public class Main { public static void main(String[] args) { ModuleGrader mg=new ModuleGrader(); System.out.println(mg.gradeModule(100)); System.out.println(mg.gradeModule(66)); } public static 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; } } } 应该是result = excellent;,以后对result = "excellent";

的所有分配

答案 1 :(得分:0)

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;
    }