Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从for循环打印元素_Java_Swing_Loops_For Loop - Fatal编程技术网

Java 从for循环打印元素

Java 从for循环打印元素,java,swing,loops,for-loop,Java,Swing,Loops,For Loop,我遇到了一个非常奇怪的问题,我有一个方法,询问游戏中的玩家数量,一旦我有了游戏中的玩家数量,我会依次询问他们的名字。一旦给了我玩家的名字,就会创建一个框架,要求他们指定他们想要选择的计数器。当他们点击紫色宝石(用于测试目的)时,它应该在控制台中打印出玩家的名字,但是for循环似乎不起作用。知道我该如何让循环正常工作吗 public class setupPlayers extends JFrame implements ActionListener { int intOfPlayers,

我遇到了一个非常奇怪的问题,我有一个方法,询问游戏中的玩家数量,一旦我有了游戏中的玩家数量,我会依次询问他们的名字。一旦给了我玩家的名字,就会创建一个框架,要求他们指定他们想要选择的计数器。当他们点击紫色宝石(用于测试目的)时,它应该在控制台中打印出玩家的名字,但是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);
        }
    }
}
公共类setupPlayers扩展JFrame实现ActionListener{
int intOfPlayers,purpleClick=0,orangeClick=0,iceClick=0,greenClick=0;
ArrayList ArrayOffLayers=新的ArrayList();
JButton purpleGemBTN、greenGemBTN、iceCubeBTN、orangeGemBTN;
JFrame组织小组;
JPanel titleChoiceCounter,副选举人;
ImageIcon finalCounter;
专用静态对话框d;
公共设置播放器(){}
公共空间层(){
试一试{
String inputValue=JOptionPane.showInputDialog(“请输入玩家数量”);
intOfPlayers=Integer.parseInt(inputValue);
如果(输入FPLayers>4){
JOptionPane.showMessageDialog(null,“只有1-4个可以播放!”、“错误!”、JOptionPane.Error\u消息);
noOfPlayers();
intOfPlayers=0;
}
对于(int z=0;z
尝试为循环编写以下内容:

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

for(intz=0;z尝试编写以下for循环:

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

for(int z=0;z您没有迭代for循环
z++
not
z=z

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

for(int z=0;z您没有迭代for循环
z++
not
z=z

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

for(int z=0;z或更好)用于每个循环

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

或者更好地使用for-each循环

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

正如你的
for(intz=0;z和
for(intz=0;z一样,从你对前面答案的评论来看,我猜你的问题更多地与你如何递归地获得名字有关

下面是一对快速的方法,它们似乎可以在没有递归的情况下实现您想要的

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);
    }
}
private ArrayList arrayOfPlayers=new ArrayList();
私有层;
公共空间层(){
while(true){
String inputValue=JOptionPane.showInputDialog(“请输入玩家数量”);
如果输入了(inputValue!=null){//Text,则不单击取消
试一试{
intOfPlayers=Integer.parseInt(inputValue);
如果(intOfPlayers>4 | | intOfPlayers<1){
JOptionPane.showMessageDialog(null,“只有1-4个可以播放!”、“错误!”、JOptionPane.Error\u消息);
}否则{
break;//不要再问数字了
}
}捕获(数字格式){
showMessageDialog(null,“请输入一个数字!”,“Error!”,JOptionPane.Error\u消息);
e、 printStackTrace();//永远不要忽略错误,即使错误很明显
}
}否则{
System.out.println(“退出数字播放器输入”);
System.exit(0);//取消了对话框,因此退出程序
}
}
对于(int z=0;z