【问题标题】:Calling a method from within an if/if else statement从 if/if else 语句中调用方法
【发布时间】:2013-04-20 15:51:53
【问题描述】:

是否可以在 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);
    }
}

任何帮助将不胜感激

【问题讨论】:

  • 是的,很有可能。非常合法:)
  • 定义方法(private void methodName(Object parameter))和调用(或调用)方法之间有一个重要的区别;你已经把它写成你想要调用的定义。
  • 有可能,但也是错误的。方法的声明应该不符合任何逻辑。您应该从 if 语句中调用它们。

标签: java if-statement methods call


【解决方案1】:

您当然可以在 if 或 else 块中调用方法。但是您在 sn-p 中尝试的是在块中声明一个方法,这是不可能的。

固定sn-p:

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 语句之外?
  • 在一个类中。如果您正在使用主要方法:在主要方法之外
  • 我还没有使用 main 方法,只是在我的 RegistryInterface 类中。我可以在 RegistryInterface 类中声明和调用该方法,还是必须从主方法中调用它?感谢您的帮助
  • 你可以在任何你想要的类中创建你的方法,并在你想要的任何类中调用它,试试这个链接知道How to create the Method
  • 我们已被指示为此分配将 main 方法放在不同的文件中;注册表应用程序。 RegistryInterface 就是我们声明方法的地方。
【解决方案2】:

调用方法是静态的

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
        }

如果调用者方法是非静态的 注册表类注册表; 无效调用方法(){ /// 这里有一些代码 扫描仪输入 = 新扫描仪(System.in); char 选择 = 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 不能从静态上下文中引用”
  • 在您的代码中 callingMethod 是静态的吗?静态方法没有得到 this 引用。 the registry should be static too 更新答案。
【解决方案3】:

你可以的。

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.");

【讨论】:

    【解决方案4】:

    在您的代码中,您不只是在 if 语句中调用方法 - 您正在尝试定义一个新方法。这是非法的。

    我猜你想要这样的东西:

    Scanner in = new Scanner (System.in);
    char choice = in.next().charAt(0);
    if(choice == '1') {
        this.theRegistry.addStudent(aStudent);
    }
    

    还请注意,您将 char choise 与 int 1 进行比较。我想您想与 char '1'

    进行比较

    【讨论】:

      【解决方案5】:

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

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

      然后

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-09
        • 1970-01-01
        • 2015-03-13
        • 2021-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-05
        相关资源
        最近更新 更多