Java 从 if/if else 语句中调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16237001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a method from within an if/if else statement
提问by Joe Perkins
Is it possible to call a method within an if statement, and then a separate method in an if else statement?
是否可以在 if 语句中调用一个方法,然后在 if else 语句中调用一个单独的方法?
I have created a scanner than reads keyboard input, and based on the option the user gives, a different method will be called. Can I say something along the lines of:
我创建了一个扫描仪而不是读取键盘输入,并且根据用户提供的选项,将调用不同的方法。我可以说一些类似的话:
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);
}
}
any help would be much appreciated
任何帮助将非常感激
采纳答案by Philipp Sander
You can of course call a method within an if or else block. But what you tried in your snippet is DECLARING a method within a block which is impossible.
您当然可以在 if 或 else 块中调用方法。但是您在代码片段中尝试的是在块中声明一个方法,这是不可能的。
fixed snippet:
固定片段:
Scanner in = new Scanner (System.in);
char choice = in.next().charAt(0);
if(choice == 1)
{
this.theRegistry.addStudent(aStudent);
}
EDIT:
编辑:
I think the code you want looks something like this:
我认为您想要的代码如下所示:
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
}
The RegistryInterface.java
RegistryInterface.java
public class RegistryInterface {
private static void doAddStudent(Student aStudent) {
this.theRegistry.addStudent(aStudent);
}
}
回答by Alya'a Gamal
Yes , create your method first , and then call them inside the if
statement , Like this:
是的,先创建你的方法,然后在if
语句中调用它们,像这样:
private static void doAddStudent(Student aStudent)
{
this.theRegistry.addStudent(aStudent);
}
then
然后
if(choice == 1)
{
doAddStudent(aStudent) ;////here you call the doAddStudent method
}
回答by rzymek
In your code you're not just calling a method inside the if
statement - you're trying to define a new method. And this is illegal.
在您的代码中,您不仅仅是在if
语句中调用一个方法- 您正在尝试定义一个新方法。这是非法的。
I'm guessing you want something like this:
我猜你想要这样的东西:
Scanner in = new Scanner (System.in);
char choice = in.next().charAt(0);
if(choice == '1') {
this.theRegistry.addStudent(aStudent);
}
Also note that you were comparing char choise
against an int 1
. I suppose you want to compare against char '1'
另请注意,您正在char choise
与 int进行比较1
。我想你想和 char 比较'1'
回答by rahul maindargi
Calling method is static
调用方法是静态的
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
}
The method called in if block Declard in same class but outside of calling method
if block 中调用的方法声明在同一个类中但在调用方法之外
private static void doAddStudent(Student aStudent)
{
theRegistry.addStudent(aStudent); // static methods do not have reference to this
}
if Caller Method is non Static TheRegistryClass theRegistry; void callingMethod(){ /// Some code here Scanner in = new Scanner (System.in); char choice = in.next().charAt(0);
如果调用方方法是非静态的 TheRegistryClass theRegistry; void callMethod(){ /// 这里有一些代码 Scanner in = new Scanner (System.in); 字符选择 = 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
}
回答by Mika
Well you can.
你可以。
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.");