Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用OO编程避免多重嵌套if';s_Java_Oop_Applet - Fatal编程技术网

Java 使用OO编程避免多重嵌套if';s

Java 使用OO编程避免多重嵌套if';s,java,oop,applet,Java,Oop,Applet,我有以下代码: public void actionPerformed(ActionEvent e) { String userInput = commandInput.getText(); if (currentLevel == 0) { if (userInput.equals(answers.getIntroAnswers().get(0)) || userInput.equals(answers.getIntroAnswers().get(1))) {

我有以下代码:

public void actionPerformed(ActionEvent e) {
    String userInput = commandInput.getText();
    if (currentLevel == 0) {
        if (userInput.equals(answers.getIntroAnswers().get(0)) || userInput.equals(answers.getIntroAnswers().get(1))) {
            messageDisplay.append("\n \n" + userInput + "\n");
            commandInput.setText("");
            messageDisplay.append("\n" + messages.getNextMessage());
            currentLevel++;
            getCurrentLevel();
        } else {
            messageDisplay.append(notValid());
        }
    } else if (currentLevel == 1) {
        // do the same as above but with the next set of answers
    }
}
我想做的是以某种方式将此操作分离到它自己的类中,并调用该类中的方法/构造函数来执行此检查,否则我将无法使用嵌套的if,它将变得非常混乱和难以理解。我认为一种获取currentLevel和userInput参数的方法是否正确,以便根据基于currentLevel的相应答案测试userInput?以下是其他相关课程的链接:

我认为一种获取currentLevel和userInput参数的方法是否正确,以便根据基于currentLevel的相应答案测试userInput

不。事实上,您可能希望避免将当前级别作为显式参数传递。如果将级别作为一个参数,则可能只会将“多个嵌套的ifs”推送到另一个类中

我想你需要这样写:

    InputChecker[] levelChecker = ... create an array of checker instances
    ....
    levelChecker[currentLevel].check(userInput);
然后您需要创建一个类(可能是匿名的)来实现对每个级别的检查。请注意,如果需要,可以通过构造函数参数将级别号提供给checker类,并将其保存在私有实例变量中

您可以扩展/概括
InputChecker
接口,以包括其他特定于级别的行为。或者确实将此部分作为
级别
接口的一部分


“这是采用当前级别并将用户输入与当前级别进行比较吗?”

否。在我上面的示例代码中,它正在调用
InputChecker
实例上的一个方法来执行检查。由于每个级别都有不同的
InputChecker
实例,因此它们可以检查不同的答案。。。或者别的什么

但是,如果每个级别的“输入检查”行为之间的唯一区别是它们根据不同的答案集进行检查,那么:

levelAnswers = answers.getAnswersForLevel(currentLevel);
for (String answer : levelAnswers) {
    if (userInput.equals(answer)) {
       // blah blah blah
    }
}

为什么不在同一个类中创建方法,而不是让另一个类来创建方法呢

        messageDisplay.append("\n \n" + userInput + "\n");
        commandInput.setText("");
        messageDisplay.append("\n" + messages.getNextMessage());
        currentLevel++;
所以我建议用同样的方法创建这个方法,然后从actionPerformed调用它

       public void checks()
       {
         String userInput = commandInput.getText();
          if (currentLevel == 0) {
            if (userInput.equals(answers.getIntroAnswers().get(0)) ||    userInput.equals(answers.getIntroAnswers().get(1))) {
              messageDisplay.append("\n \n" + userInput + "\n");
              commandInput.setText("");
              messageDisplay.append("\n" + messages.getNextMessage());
              currentLevel++;
              getCurrentLevel();
            } else {
                messageDisplay.append(notValid());
               }
           } else if (currentLevel == 1) {
                // do the same as above but with the next set of answers
               }
        }
     public void actionPerformed(ActionEvent e)
     {
       check():
     }
然后从actionPerformed调用它

       public void checks()
       {
         String userInput = commandInput.getText();
          if (currentLevel == 0) {
            if (userInput.equals(answers.getIntroAnswers().get(0)) ||    userInput.equals(answers.getIntroAnswers().get(1))) {
              messageDisplay.append("\n \n" + userInput + "\n");
              commandInput.setText("");
              messageDisplay.append("\n" + messages.getNextMessage());
              currentLevel++;
              getCurrentLevel();
            } else {
                messageDisplay.append(notValid());
               }
           } else if (currentLevel == 1) {
                // do the same as above but with the next set of answers
               }
        }
     public void actionPerformed(ActionEvent e)
     {
       check():
     }

所以现在你可以用一个单独的方法来处理if。

在我看来,既然你谈论了很多级别,你可能应该有一个表示级别的类。事实上,因为你显然有不止一个级别,这两个级别的行为稍有不同,所以你有两种方法

  • 有一个级别接口,然后为每个级别创建一个类
  • 拥有一个级别类,其构造函数隐藏该类中的级别号
  • 在这之后,您可以多态地切换,而不是嵌套的if语句(或者if的近亲switch语句)


    我确实希望if在另一个类中,因为它所在的类是用于Applet的,我正在努力保持该类的干净性,并且只用于设置。>levelChecker[currentLevel]。检查(用户输入);这是采用当前级别并将用户输入与当前级别进行比较吗?因为userInput需要根据每条消息的答案数组进行检查,所以我认为这不起作用,这就是为什么我考虑传入currentLevel,因为它可以被引用到正确的消息,然后可以根据与该消息匹配的答案检查userInput。我得到了您的答案是肯定的,但我不知道如何实际使用每一位。我认为我把它复杂化了,但是你能不能把Git回购协议分支,并用评论的方式来做呢?