在if/if else语句中调用一个方法

3

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

任何帮助都会非常感激

是的,这是完全可能的。而且非常合法 :) - Satya
2
定义一个方法(private void methodName(Object parameter))和调用方法之间有一个重要的区别;您已将其编写为定义,而您需要的是调用。 - Mikeb
这是可能的,但也是错误的。方法应该在任何逻辑之外声明。您应该从if语句中调用它们。 - aran
5个回答

6
当然可以在if或else块中调用一个方法,但是你在代码片段中尝试的是在一个块内部声明一个方法,这是不可能的。
修正后的代码片段:
Scanner in = new Scanner (System.in);
char choice = in.next().charAt(0);

if(choice == 1)
{
    this.theRegistry.addStudent(aStudent);
}

编辑:

我认为你想要的代码应该是这样的:

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
}

RegistryInterface.java文件
public class RegistryInterface {
    private static void doAddStudent(Student aStudent) {
        this.theRegistry.addStudent(aStudent);
    }
}

我明白你的意思,但是我应该在哪里声明这个方法?在if语句之外吗? - Joe Perkins
1
你可以在任何类中创建自己的方法,并在任何类中调用它。请尝试使用此链接了解如何创建方法:Java 方法教程 - Alya'a Gamal
@PhilippSander:我认为你需要在调用RegistryInterface时创建一个对象,例如:RegistryInterface r = new RegistryInterface(); r.doAddStdent(student);??? - Alya'a Gamal
不过,如果RegistryInterface类中的方法是静态的,则不需要这样做。但是有可能赋值语句告诉他这样做;-)但我无法知道;-)那么你的代码就是正确的。 - Philipp Sander
还有一件事,抱歉。我使用了你给我的代码,但是在RegistryApp中出现了一个错误,它无法在RegistryInterface.doAddStdent(student)这一行中找到符号aStudent。 - Joe Perkins
显示剩余6条评论

2
你是可以做到的。
Scanner in = new Scanner (System.in);
char choice = in.next().charAt(0);

if(choice == 1)
    this.theRegistry.addStudent(aStudent);
else if choice == 2)
    this.theRegistry.removeStudent(aStudent);
else
    System.out.println("Please enter a valid choice.");

1

是的,先创建你的方法,然后在if语句内调用它们,像这样:

private static void doAddStudent(Student aStudent) 
        {
            this.theRegistry.addStudent(aStudent);
        }

然后。
 if(choice == 1)
    {
        doAddStudent(aStudent) ;////here you call the doAddStudent method

    }

1
在你的代码中,你不仅仅是在if语句中调用一个方法 - 你试图定义一个新方法。这是非法的。 我猜你想要的是这样的:
Scanner in = new Scanner (System.in);
char choice = in.next().charAt(0);
if(choice == '1') {
    this.theRegistry.addStudent(aStudent);
}

还要注意的是,你正在将 char choise 与整数 1 进行比较。我想你想要将其与字符 '1' 进行比较。

1
调用该方法是静态的。
static TheRegistryClass theRegistry;
static void callingMethod(){
/// Some code here 
Scanner in = new Scanner (System.in);
    char choice = in.next().charAt(0);

    if(choice == 1)
    {
       doAddStudent(aStudent);
    }

//remaining code here 

}

在if块中调用的方法在同一类中声明,但不在调用方法内部。
 private static void doAddStudent(Student aStudent) 
        {
            theRegistry.addStudent(aStudent); // static methods do not have reference to this
        }

如果调用方法不是静态的 TheRegistryClass theRegistry; void callingMethod(){ /// 这里有一些代码 Scanner in = new Scanner (System.in); char choice = in.next().charAt(0);

    if(choice == 1)
    {
       doAddStudent(aStudent);
    }

//remaining code here 

}



 private static void doAddStudent(Student aStudent) 
        {
            this.theRegistry.addStudent(aStudent); // this is optional
        }

当我输入这个时,我会得到一个错误:“非静态变量this无法从静态上下文中引用”。 - Joe Perkins
你的代码中的callingMethod是静态的吗?静态方法不会获取this引用。更新后的答案是:注册表也应该是静态的。 - rahul maindargi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接