【问题标题】:How to get a method to run a certain amount of times如何让方法运行一定次数
【发布时间】:2016-08-03 02:39:38
【问题描述】:

我用 java 制作了一个游戏,但它只运行一轮。我希望它能够根据对话框中按下的数字运行一定次数。我认为答案是在一场比赛结束后运行该方法,尽管我不确定如何执行此操作。

jframe的创建及方法

public Rockgame() {

    // JFrame
    p = new ImagePanel(Toolkit.getDefaultToolkit().getImage("space.jpg"));
    f = new JFrame("SpaceMiners");

    f.setSize(700, 500);
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

决定运行次数的按钮:

Object[] options = { "Three", "Five", "Ten" };
    no_of_games = (int) JOptionPane.showOptionDialog(f,
            "Would you like to play best of:", "Rounds" + "",
            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
            null, options, options[2]);

if (no_of_games == 0) {
        no_of_games = no_of_games + 3;
    }
    if (no_of_games == 1) {
        no_of_games = no_of_games + 4;
    }
    if (no_of_games == 2) {
        no_of_games = no_of_games + 8;
    }

创建网格和鼠标监听器

p.setLayout(new GridLayout(10, 10));
    f.getContentPane().add(p, BorderLayout.CENTER);

    for (int x = 0; x < 10; x++) {

        for (int y = 0; y < 10; y++) {

            playingGrid[x][y] = new JLabel(new ImageIcon("rock.png"));
            p.add(playingGrid[x][y]);

            playingGrid[x][y].addMouseListener(new Rockbreaker());

        }
    }

}

public class Rockbreaker implements MouseListener {
    // manbitesdog6
    public void mouseClicked(MouseEvent e) {
        // sets all columns greater than one clicked to invisible

            if (e.getSource() == playingGrid[0][0]){
                for(int u =0;u<10;u++){
                for(int y =0;y<10;y++){
                Rockgame();

                }
            }
            }

            for (int x = 0; x < 10; x++) {
                for (int y = 0; y < 10; y++) {
                    if (playingGrid[x][y] == e.getSource()) {
                        for (int k = 0; k < 10; k++) {
                            for (int i = 0; i < 10; i++) {
                                if ((i >= x) && (k >= y)) {
                                    playingGrid[i][k].setVisible(false);
                                }
                            }
                        }

                    }
                }
            }

【问题讨论】:

  • 是函数中的 = 一轮(你想要 3、5 或 10 轮)的代码还是到处都是? TL;DR 我可以调用类似 startRound()
  • 他们点击游戏的部分在鼠标监听器中,我认为不是
  • RockGame() 函数中有什么?
  • 在 rockGame() 方法中创建网格并为其提供图像以及 jframe 等。然后在我添加到播放网格的 mouselistener 中,我有一个循环,使在网格上消失。当我单击左上角的那个时,我想为另一轮重置网格。如果这有意义
  • 当用户输入回合数时赚大钱?

标签: java swing methods jpanel


【解决方案1】:

尝试使用for()-loop。

for(int i = 0; i < no_of_games; i++) {
     /*
      * Start game
      * Reset grid / panel
      * ...
      */
}

另外,看看这个:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

【讨论】:

  • 我试过了,但它只是说 for 循环什么也没做。
  • 是的,因为在我的示例代码的循环中只有一个注释。你当然应该自己实现你的代码,我只是给你一个 for() 循环的示例代码,它目前什么都不做。我不会为你实现 - 现在轮到你了......
猜你喜欢
  • 2021-06-06
  • 2012-03-28
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 2016-12-20
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
相关资源
最近更新 更多