Java彩票游戏效率,摆动

时间:2017-09-08 03:30:33

标签: java swing

我相信这不是我应该问的问题,所以我提前道歉。我在2天内参加了一个考试,应该给我一个课程的老师清除我的所有问题都要取消,所以现在我没有人要问。我正在解决去年的考试,其中一个问题是制作一个小程序,我们几乎模拟了6个数字的彩票游戏。我的问题是关于效率,考试写在纸上,我觉得如果我写的是我在电脑上输入的所有内容我没有足够的时间,我们有90分钟,这只是其中一个问题,摇摆不是我的强项让我觉得我可以用更少的线来写这个。公共实例变量' key'是否模拟来自另一个类的方法,该方法生成我们不需要实现的随机密钥。我们只允许使用FlowLayout,BorderLayout,CardLayout和GridLayout。

公共类Grupo2A扩展了JFrame {

private JPanel panelCont = new JPanel();
private JPanel panelUser = new JPanel();
private JPanel panelResult = new JPanel();
private JPanel p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11;
private JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9;
private JTextField t1, t2, t3, t4, t5, t6;
private JButton b1, b2, b3;
private int[] userKey = new int[6];
private CardLayout cl = new CardLayout();
private GridLayout gl1 = new GridLayout(8, 1);
private GridLayout gl2 = new GridLayout(3, 1);
public int[] key = {12, 13, 16, 22, 33, 40};

private static final int WINDOW_HEIGHT1 = 600;
private static final int WINDOW_HEIGHT2 = 150;
private static final int WINDOW_WIDTH1 = 300;

public Grupo2A() {
    panelCont.setLayout(cl);
    panelCont.add(panelUser, "panelUser");
    panelCont.add(panelResult, "panelResult");
    panelUser.setLayout(gl1);
    panelResult.setLayout(gl2);
    setSize(WINDOW_WIDTH1, WINDOW_HEIGHT1);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    add(panelCont);

    createComponents();
    createPanelUser();
    createPanelResult();
    cl.show(panelCont, "panelUser");
    setVisible(true);
}

private void createComponents() {

    l1 = new JLabel("Lottery");
    l2 = new JLabel("1");
    l3 = new JLabel("2");
    l4 = new JLabel("3");
    l5 = new JLabel("4");
    l6 = new JLabel("5");
    l7 = new JLabel("6");

    t1 = new JTextField(10);
    t2 = new JTextField(10);
    t3 = new JTextField(10);
    t4 = new JTextField(10);
    t5 = new JTextField(10);
    t6 = new JTextField(10);

    b1 = new JButton("Play");
    b1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            int[] userKey = new int[6];
            userKey[0] = Integer.parseInt(t1.getText());
            userKey[1] = Integer.parseInt(t2.getText());
            userKey[2] = Integer.parseInt(t3.getText());
            userKey[3] = Integer.parseInt(t4.getText());
            userKey[4] = Integer.parseInt(t5.getText());
            userKey[5] = Integer.parseInt(t6.getText());
            setUserKey(userKey);
            l8.setText("You got " + correctNumbers() + " numbers");
            l9.setText("The key was: " + Arrays.toString(key));
            cl.show(panelCont, "panelResult");
            setSize(WINDOW_WIDTH1, WINDOW_HEIGHT2);
        }

    });
    b2 = new JButton("Clear");
    b2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            t1.setText("");
            t2.setText("");
            t3.setText("");
            t4.setText("");
            t5.setText("");
            t6.setText("");
        }

    });

    p1 = new JPanel();
    p2 = new JPanel();
    p3 = new JPanel();
    p4 = new JPanel();
    p5 = new JPanel();
    p6 = new JPanel();
    p7 = new JPanel();
    p8 = new JPanel();
    p9 = new JPanel();
    p10 = new JPanel();
    p11 = new JPanel();

}

private void createPanelUser() {
    p1.add(l1);
    panelUser.add(p1);
    p2.add(l2);
    p2.add(t1);
    panelUser.add(p2);
    p3.add(l3);
    p3.add(t2);
    panelUser.add(p3);
    p4.add(l4);
    p4.add(t3);
    panelUser.add(p4);
    p5.add(l5);
    p5.add(t4);
    panelUser.add(p5);
    p6.add(l6);
    p6.add(t5);
    panelUser.add(p6);
    p7.add(l7);
    p7.add(t6);
    panelUser.add(p7);
    p8.add(b1);
    p8.add(b2);
    panelUser.add(p8);
}

private void createPanelResult() {
    l8 = new JLabel("You got " + correctNumbers() + " numbers");
    l9 = new JLabel("The key was: " + Arrays.toString(key));
    b3 = new JButton("Ok");
    b3.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            cl.show(panelCont, "panelUser");
            setSize(WINDOW_WIDTH1, WINDOW_HEIGHT1);
        }

    });
    p9.add(l8);
    p10.add(l9);
    p11.add(b3);
    panelResult.add(p9);
    panelResult.add(p10);
    panelResult.add(p11);
}

private int correctNumbers() {
    int cont = 0;
    for (int i = 0; i < userKey.length; i++) {
        for (int j = 0; j < key.length; j++) {
            if (userKey[i] == key[j]) {
                cont++;
                break;
            }
        }
    }
    System.out.println(cont);
    return cont;
}

private void setUserKey(int[] userKey) {
    this.userKey = userKey;
}

}

1 个答案:

答案 0 :(得分:0)

我真的希望你没有被要求在考试中写一篇论文用户界面。考虑到它并不像你在实践中需要做的任何事情,这将是一种非常愚蠢的方式来测试你的知识。

有很多方法可以提高代码的可读性。但就清晰和简洁而言,关键原则是干:不要重复自己。

例如,您已声明并初始化了6个面板,标签和字段,代码几乎完全相同。这几乎总是一个标志,你应该将你的代码封装在一个单独的类中。类似的东西:

private class NumberPanel extends JPanel {
    private final JLabel label;
    private final JTextField field;

    public NumberPanel(int place) {
        super(new BorderLayout());
        this.label = new JLabel(Integer.toString(place));
        this.field = new JTextField(10);
        add(label, BorderLayout.WEST);
        add(field, BorderLayout.EAST);
    }

    public int getValue() {
        return Integer.valueOf(field.getText());
    }
}

private final List<NumberPanel> numberPanels =
    IntStream.rangeClosed(1, 6)
        .mapToObj(NumberPanel::new).collect(toList());
相关问题