简体   繁体   English

从另一个方法调用一个方法

[英]Calling a method from another method

I know the problem exists on the forum, but I am really stuck.我知道论坛上存在问题,但我真的被卡住了。 I would like to call my grade() method into my display() method to appear the grade .我想将我的grade()方法调用到我的display()方法中以显示Grade I don't know how to do...我不知道该怎么办...

Here is my grade() method这是我的grade()方法

    public static void grade(int note){
            String grade = "";
            if (note <= 70){
                grade = "Satisfaction";
            }
            else{
                grade = "Great Satisfaction";
            }

    }

In my display() method, I want to retrieve the grade for the student.在我的display()方法中,我想检索学生的成绩

public static void display(String[] tabStudent, int[] tabNote, char code){

        for(int i=0; i<tabStudent.length;i++){
            if (code == 'e'){
                if (tabNote[ i ] <  50){
                    System.out.println(tabStudent[i] +  " " + tabNote[i] );
                }
            }
            else if(code == 's'){
                if (tabNote[ i ] > 50){
                    System.out.println(tabStudent[i] +  " " + tabNote[i] + grade() );
                }
            }
        }

    }

My problem is this line below:我的问题是下面这一行:

System.out.println(tabStudent[i] +  " " + tabNote[i] + grade() );

Here is an image这是一张图片

Thank you for your help.感谢您的帮助。

tl;dr you're missing a return statement tl;dr 你缺少一个 return 语句


Imagine you have a bunch of people around you who can either do a task (hopefully...) perfectly or can calculate some value (again, hopefully...) perfectly.想象一下,你身边有一群人,他们可以完美地完成一项任务(希望……),或者可以完美地计算一些价值(再次,希望……)。 When you tell one person, "go put coins into a vending machine", they go do it and walk back.当你告诉一个人“去把硬币放进自动售货机”时,他们去做然后走回去。 They don't say anything, they just kind of...go.他们什么也没说,他们只是有点……走。 At most, you might get an "Ok" or a "HEY SOMETHING WENT WRONG" when they come back but that's the extent of their verbal capacity.最多,当他们回来时,你可能会得到一个“好的”或“嘿,出了点问题”,但这就是他们语言能力的程度。

On the other hand, you have people who you ask "What are the 1st through 3rd letters of 'hello'?"另一方面,有些人会问“'hello' 的第 1 到第 3 个字母是什么?” And they will say, "'el'."他们会说,“'el'。” They may go grab a calculator or a book to figure out the answer, but they can do it and will report the value back to you .他们可能会去拿一个计算器或一本书来找出答案,但他们可以做到,并且会将价值报告给您

The former is an example of a void function, while the latter is what you get when you return from a function.前者是 void 函数的一个例子,而后者是你从函数返回时得到的。 When you have a function simply calculate values in a void function, it's equivalent to asking a friend, "What's 5+3?", them doing the calculation in their head and saying "done! :D".当你有一个函数只是计算一个 void 函数中的值时,这相当于问一个朋友,“什么是 5+3?”,他们在头脑中进行计算并说“完成!:D”。 Not super helpful.不是超级有帮助。

While there are ways to get around needing to return while not having to literally return (such as using global variables), they are heavily frowned up much like passing notes to relay information was in middle school by your teacher.虽然有一些方法可以避免需要返回而不必真正返回(例如使用全局变量),但它们非常不受欢迎,就像你的老师在中学通过笔记传递信息一样。

Well, it needs to actually return something:好吧,它实际上需要返回一些东西:

public static String grade(int note){
  String grade = "";

  if (note <= 70) {
    grade = "Satisfaction";
  } else {
    grade = "Great Satisfaction";
  }

  return grade;
}

I can see two issues in your code: 1. you should change return type of grade() method and return grade.我可以在您的代码中看到两个问题: 1. 您应该更改 Grade() 方法的返回类型并返回等级。 2. Your grade() method expect one parameter but you are not passing any while calling it. 2. 您的 grade() 方法需要一个参数,但您在调用它时没有传递任何参数。 Try below code.试试下面的代码。

public static String grade(int note){
        String grade = "";
        if (note <= 70){
            grade = "Satisfaction";
        }
        else{
            grade = "Great Satisfaction";
        }
        return grade;
}


 public static void display(String[] tabStudent, int[] tabNote, char code){

    for(int i=0; i<tabStudent.length;i++){
        if (code == 'e'){
            if (tabNote[ i ] <  50){
                System.out.println(tabStudent[i] +  " " + tabNote[i] );
            }
        }
        else if(code == 's'){
            if (tabNote[ i ] > 50){
                System.out.println(tabStudent[i] +  " " + tabNote[i] + " " + grade(tabNote[i]) );
            }
        }
    }

}

The first problem is that you do not have a method grade() .第一个问题是您没有方法grade() Instead, you have defined grade(int note) which expects an integer.相反,您定义了期望整数的grade(int note)

Even if you correct this problem and call grade like即使你纠正了这个问题并称grade

System.out.println(tabStudent[i] +  " " + tabNote[i] + grade(80));

it won't work because you have declared grade as它不起作用,因为您已将grade声明为

public static void grade(int note)

which should be这应该是

public static String grade(int note)

ie return type should be String instead of void as per your requirement.即根据您的要求,返回类型应该是String而不是void

Define it as将其定义为

public static String grade(int note){
    String grade = "";
    if (note <= 70){
        grade = "Satisfaction";
    }
    else{
        grade = "Great Satisfaction";
    }
    return grade;
}

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

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