【问题标题】:Thinking outLoud Conditional if statement Vs. For statement大声思考条件 if 语句与。对于声明
【发布时间】:2014-04-30 05:42:15
【问题描述】:

我已经使用 java 一年多了。我最近构建了一个井字游戏作为我的 Java 课程的作业。讲师评分后,他围绕我的验证方法逻辑写了一条评论。尽管我得到了 100%,但他说我的验证方法中的逻辑太麻烦了。他说我研究了一个 for 语句或一个 while 语句,以便在我的验证方法中清除一些代码。这是我的问题,真的有办法将我所有的条件 if 语句放入 for 循环或 while 循环吗?如果是这样,我想知道这背后的逻辑是什么。这个程序有一组五个数组,但在这个验证方法中,我只使用了 JButton 数组。

JButton button = new Jbutton[9];

public void validate()
    {
        if(button[0].getText().equals(button[1].getText()) && button[1].getText().equals(button[2].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
            gameOver();
            return;
        }
        else if(button[3].getText().equals(button[4].getText()) && button[4].getText().equals(button[5].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[3].getText());
            gameOver();
            return;
        }
        else if(button[6].getText().equals(button[7].getText()) && button[7].getText().equals(button[8].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[6].getText());
            gameOver();
            return;
        }
        else if(button[0].getText().equals(button[3].getText()) && button[3].getText().equals(button[6].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
            gameOver();
            return;
        }
        else if(button[1].getText().equals(button[4].getText()) && button[4].getText().equals(button[7].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[1].getText());
            gameOver();
            return;
        }
        else if(button[1].getText().equals(button[4].getText()) && button[4].getText().equals(button[7].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[1].getText());
            gameOver();
            return;
        }
        else if(button[2].getText().equals(button[5].getText()) && button[5].getText().equals(button[8].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[2].getText());
            gameOver();
            return;
        }
        else if(button[0].getText().equals(button[4].getText()) && button[4].getText().equals(button[8].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
            gameOver();
            return;
        }
        else if(button[2].getText().equals(button[4].getText()) && button[4].getText().equals(button[6].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[2].getText());
            gameOver();
            return;
        }

        int i;

        for(i=0;i<button.length;i++)
        {
            if(button[i].isEnabled())
            {
                break;
            }
        }


        if(i == button.length)
        {
            JOptionPane.showMessageDialog(null,"This was a Draw");
        }
    }

【问题讨论】:

  • 您进行清理的第一个简单步骤是提取一个显示return button[i].getText().equals(button[j].getText()) &amp;&amp; button[j].getText().equals(button[k].getText());的方法
  • 好吧 摆脱那是你是对的最好的方法。是否应该做一个 for 语句,如果是的话,怎么可能将这么多条件放在一个中?
  • 我总是一步一个脚印。执行上述操作,然后将 then-block 提取到另一个方法中;那么您将拥有一系列紧凑的 else-if,其中只有数字发生变化。然后你可能会想出更多的东西。
  • 同时提取消息和gameover 方法。然后看看你是否有一些 i/j/k 的模式......类似于进程......进程可以用循环(for,while)表示。

标签: java for-loop while-loop conditional


【解决方案1】:

如果有一种方法可以使用按钮组合的数学级数来找到获胜者,那么您可能会在循环中提取支票。

您还可以通过提取重复任务来重构代码,例如(如已经建议的那样)这样做:

  • 为单独的函数中的按钮提取if 条件
  • 在单独的函数中提取消息
  • 在单独的函数中提取返回索引并调用gameOver() 并且只返回一次

您的代码将变得更易读且更短:

    private Boolean checkConditionFor(int button1, int button2, int button3) {
        return button[button1].getText().equals(button[button2].getText()) 
            && button[button2].getText().equals(button[button3].getText());
    }

    private void messageFor(int id) {
        JOptionPane.showMessageDialog(null, "Thank you the winner is" + button[id].getText());
        gameOver();
    }

    private int winner() {
        if(checkConditionFor(0, 1, 2) || 
           checkConditionFor(0, 3, 6) || 
           checkConditionFor(0, 4, 8)) { return 0; }
        if(checkConditionFor(3, 4, 5)) { return 3; }
        if(checkConditionFor(6, 7, 8)) { return 6; }
        if(checkConditionFor(1, 4, 7)) { return 1; }
        if(checkConditionFor(2, 5, 8) || 
           checkConditionFor(2, 4, 6)) { return 2; }
    }

现在只需调用validate 方法中的函数:

    messageFor(winner());
    return;

【讨论】:

    猜你喜欢
    • 2016-05-27
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多