【问题标题】:Java lottery game efficiency, swing [closed]Java彩票游戏效率,摇摆[关闭]
【发布时间】:2017-09-08 03:30:32
【问题描述】:

我认为这不是我应该在这里问的问题,所以我提前道歉。两天后我要考试了,本来应该给我上课的老师把我所有的问题都取消了,所以现在我没有人可以问了。我正在解决去年的考试,其中一个问题是制作一个小的 Java 程序,我们几乎可以在其中模拟 6 个数字的彩票游戏。我的问题是关于效率的,考试是写在纸上的,我觉得如果我要把我在电脑上输入的所有东西都写下来,我会没有足够的时间,我们有 90 分钟,这只是问题之一,swing 不是我的强项,所以这让我认为我可以用更少的行来写这个。公共实例变量“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;
}

}

【问题讨论】:

  • 不要只是用你的代码转储来膨胀网站,指出细节并提出可读的问题。
  • 你的怀疑是正确的,这种问题不属于本站。
  • 你的问题是?如果您担心时间,请从设计图开始,标记各个部分并展示您如何在代码中完成它们,您将使用什么以及为什么
  • 我的建议:如果您在考试中不得不在纸上编写 Java 代码:1) 不要实现 GUI(如果可以避免的话)和 2) 省去繁琐的部分;例如样板的 getter 和 setter。 (但包括一条评论,说明 getter/setter 的去向。)
  • 我还认为,如果你被要求用铅笔和纸实现 GUI,那么面试官/考试设置者需要重新教育。让考生使用带有 IDE 的计算机。

标签: java swing


【解决方案1】:

我真的希望您不要在考试中被要求在纸上写 UI。这将是测试您的知识的一种非常愚蠢的方式,因为它与您在实践中需要做的任何事情都不一样。

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

例如,您已经用几乎相同的代码声明并初始化了 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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    相关资源
    最近更新 更多