从if/if else语句中调用方法

Joe*_*ins 3 java methods if-statement call

是否可以在if语句中调用方法,然后在if else语句中调用单独的方法?

我创建了一个扫描仪而不是读取键盘输入,并根据用户提供的选项,将调用另一种方法.我可以说一下以下内容:

Scanner in = new Scanner (System.in);
char choice = in.next().charAt(0);

if(choice == 1)
{
    private static void doAddStudent(Student aStudent) 
    {
        this.theRegistry.addStudent(aStudent);
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激

Phi*_*der 6

您当然可以在if或else块中调用方法.但是你在你的代码片段中尝试的是在一个块中声明一个不可能的方法.

固定代码段:

Scanner in = new Scanner (System.in);
char choice = in.next().charAt(0);

if(choice == 1)
{
    this.theRegistry.addStudent(aStudent);
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我认为你想要的代码看起来像这样:

public static void main(String[] args) {
    //some code
    Scanner in = new Scanner (System.in);
    char choice = in.next().charAt(0);

    if(choice == 1)
    {
        RegistryInterface.doAddStdent(student);
    }
    //some code
}
Run Code Online (Sandbox Code Playgroud)

RegistryInterface.java

public class RegistryInterface {
    private static void doAddStudent(Student aStudent) {
        this.theRegistry.addStudent(aStudent);
    }
}
Run Code Online (Sandbox Code Playgroud)