从for循环中打印元素

时间:2015-12-22 00:03:48

标签: java swing loops for-loop

我有一个非常奇怪的问题,我有一个方法noOfPlayers要求游戏中的玩家数量,一旦我有游戏中的玩家数量,我依次要求他们的每个名字。一旦我获得了玩家的名字,就会创建一个框架,要求他们指定他们想要选择的计数器。当他们点击紫色宝石(用于测试目的)时,它应该在控制台中打印出玩家名称,但for循环似乎不起作用。知道如何让循环正常工作吗?

public class setupPlayers extends JFrame implements ActionListener {
    int intOfPlayers, purpleClick = 0, orangeClick = 0, iceClick = 0, greenClick = 0;
    ArrayList<Player> arrayOfPlayers = new ArrayList<Player>();
    JButton purpleGemBTN, greenGemBTN, iceCubeBTN, orangeGemBTN;
    JFrame organisationPanel;
    JPanel titleChoiceCounter, counterSelector;
    ImageIcon finalCounter;
    private static Dialog d;

    public setupPlayers() {}

    public void noOfPlayers() {
        try {
            String inputValue = JOptionPane.showInputDialog("Please input the number of players");
            intOfPlayers = Integer.parseInt(inputValue);
            if (intOfPlayers > 4) {
                JOptionPane.showMessageDialog(null, "Only 1-4 can play!", "Error!", JOptionPane.ERROR_MESSAGE);
                noOfPlayers();
                intOfPlayers = 0;
            }

            for (int z = 0; z < intOfPlayers; z++) {
                String playerName = JOptionPane.showInputDialog("Player " + (z + 1) + " please input your name");
                chooseCounter();
                arrayOfPlayers.add(new Player(playerName, (z + 1), null, 0));
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "You did not enter the number of players, please enter the number of players", "Error!", JOptionPane.ERROR_MESSAGE);
            noOfPlayers();
        }
    }

    public void chooseCounter() {
        Frame window = new Frame();

        ImageIcon purpleGemImg = new ImageIcon("C:\\Users\\Anonymous\\Documents\\pink.png");
        ImageIcon greenGemImg = new ImageIcon("C:\\Users\\Anonymous\\Documents\\yellow.png");
        ImageIcon orangeGemImg = new ImageIcon("C:\\Users\\Anonymous\\Documents\\brown.png");
        ImageIcon iceCubeImg = new ImageIcon("C:\\Users\\Anonymous\\Documents\\white.png");

        d = new Dialog(window, "Please select your counter", true);
        d.setLayout(new GridLayout(2, 2));
        d.setLocation(400, 300);
        d.setSize(500, 500);

        purpleGemBTN = new JButton("purple", purpleGemImg);
        greenGemBTN = new JButton(greenGemImg);
        orangeGemBTN = new JButton(orangeGemImg);
        iceCubeBTN = new JButton(iceCubeImg);

        purpleGemBTN.addActionListener(this);
        greenGemBTN.addActionListener(this);
        iceCubeBTN.addActionListener(this);
        orangeGemBTN.addActionListener(this);

        d.add(purpleGemBTN);
        d.add(greenGemBTN);
        d.add(orangeGemBTN);
        d.add(iceCubeBTN);

        d.setVisible(true);
    }

    public static void main(String[] args) {
        setupPlayers spObj = new setupPlayers();
    }

    public void actionPerformed(ActionEvent e) {
        JButton pressed = new JButton();
        pressed = (JButton) e.getSource();
        if (pressed.getText().equals("purple")) {
            for (int z = 0; z < arrayOfPlayers.size() - 1; z = z) {
                String currentPlayer = arrayOfPlayers.get(z).playerNme;
                System.out.println(currentPlayer);
            }
            d.setVisible(false);
        }
    }
}

5 个答案:

答案 0 :(得分:2)

尝试写这个for循环:

  for (int z=0; z<arrayOfPlayers.size()-1;z=z){...

像这样:

   for (int z=0; z<arrayOfPlayers.size();z++){...

z++增加-1并删除arrayOfPlayers.size()-1,因为索引z0开始:

答案 1 :(得分:2)

每个循环

或更好

    for(Player p : arrayOfPlayers)
    {
      String name = p.playerNme;
      System.out.println(name);
    }

答案 2 :(得分:0)

您没有遍历for循环z++而不是z=z

for (int z=0; z<arrayOfPlayers.size()-1;z++){
    String currentPlayer = arrayOfPlayers.get(z).playerNme;
    System.out.println(currentPlayer);

答案 3 :(得分:0)

如果您for (int z=0; z<arrayOfPlayers.size()-1;z=z),您将无限迭代,因为z将始终等于0

尝试将z=z更改为z++,以便迭代结束z = arrayOfPlayers.size()-1

您的代码将是:

for (int z=0; z<arrayOfPlayers.size();z++){
    String currentPlayer = arrayOfPlayers.get(z).playerNme;
    System.out.println(currentPlayer);
}

答案 4 :(得分:0)

因此,根据您对之前答案的评论,我猜测您的问题与递归获取名称的方式有关。

这是一对快速的方法,似乎可以在没有递归的情况下实现您想要的功能。

private ArrayList<Player> arrayOfPlayers = new ArrayList<>();
private int intOfPlayers;

public void noOfPlayers() {

    while (true) {
        String inputValue = JOptionPane.showInputDialog("Please input the number of players");

        if (inputValue != null) { // Text was entered, cancel not clicked
            try {
                intOfPlayers = Integer.parseInt(inputValue);
                if (intOfPlayers > 4 || intOfPlayers < 1) {
                    JOptionPane.showMessageDialog(null, "Only 1-4 can play!", "Error!", JOptionPane.ERROR_MESSAGE);
                } else {
                    break; // stop asking for numbers
                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Please enter a number!", "Error!", JOptionPane.ERROR_MESSAGE);
                e.printStackTrace(); // never ignore errors, even if obvious
            }
        } else {
            System.out.println("Quitting from number players input");
            System.exit(0); // Canceled the dialog, so quit the program
        }

    }

    for (int z = 0; z < intOfPlayers; z++) {
        String playerName = JOptionPane.showInputDialog("Player " + (z + 1) + " please input your name");
        if (playerName != null) {
            // chooseCounter(playerName);
            arrayOfPlayers.add(new Player(playerName, (z + 1), null, 0));
        } else {
            System.out.println("Quitting from player " + (z + 1) + " name input");
            System.exit(0); // Canceled the dialog, so quit the program
        }
    }

    printPlayerNames();
}

private void printPlayerNames() {
    for (int z = 0; z < arrayOfPlayers.size(); z++) {
        String currentPlayer = arrayOfPlayers.get(z).playerNme;
        System.out.println(currentPlayer);
    }
}