java - java中for循环数组只处理一个元素?

标签 java arrays loops for-loop

我不明白为什么每当我使用 for 循环循环遍历数组时,它只生成一个元素(第一个)来控制台?我很确定这是我正在检查的菜鸟错误,因此任何提示和建议都会有所帮助。

我正在编写一个有趣的程序,它比较在文本字段中键入的两个字符串,如果它们不存在于数组中,则会生成相反的 JOPtionPane 消息。这是为了我将来可能为 vBulletin 论坛制作的战斗黑客,但在进行这一步之前我正在搞乱算法。谢谢大家!

package battleoptionspart1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
import javax.swing.border.*;


public class BattleOptionsPart1 extends JFrame{

JButton newthread, previewpost;
JRadioButton battle1;
JTextField postcount, oppA, oppB;
JLabel battle2, max;
JPanel panel;
String [] array = {"Bill","Tom","Wendy", "Paula"};


 public BattleOptionsPart1 () {

        panel = new JPanel();

    Toolkit tool = Toolkit.getDefaultToolkit();
    Dimension dim = tool.getScreenSize();
    this.setSize(500, 500);
    this.setTitle("Battle Options");

    GridLayout grid = new GridLayout(0,1,2,2);
    this.setLayout(grid);

    newthread = new JButton("Post New Thread");
    previewpost = new JButton("Preview Post");
    postcount = new JTextField("", 4);
    oppA = new JTextField("",10);
    oppB = new JTextField("",10);
    battle1 = new JRadioButton();
    battle2 = new JLabel("Would you like to start a recorded battle?");
    max = new JLabel("Enter max post count user must have to vote");


    ListenForButton listen = new ListenForButton();

    newthread.addActionListener(listen);
    previewpost.addActionListener(listen);

    JPanel opponents = new JPanel();
    Border oppBorder = BorderFactory.createTitledBorder("Battlers");
    opponents.setBorder(oppBorder);
    opponents.add(oppA);
    opponents.add(oppB);


    JPanel battle = new JPanel();
    Border battleBorder = BorderFactory.createTitledBorder("Start Battle");
    battle.setBorder(battleBorder);
    battle.add(battle1);
    battle.add(battle2);

    JPanel buttons = new JPanel();
    Border buttonBorder = BorderFactory.createTitledBorder("Create Thread");
    buttons.setBorder(buttonBorder);
    buttons.add(newthread);
    buttons.add(previewpost);

    JPanel restriction = new JPanel();
    Border resBorder = BorderFactory.createTitledBorder("Restrictions");
    restriction.setBorder(buttonBorder);
    restriction.add(postcount);
    restriction.add(max);


    this.add(opponents);
    this.add(battle);
    this.add(restriction);
    this.add(buttons);


    this.add(panel);
    int xPos = (dim.width / 2) - (this.getWidth() / 2);
  int yPos = (dim.height / 2) - (this.getHeight() / 2);
  this.setLocation(xPos,yPos); //places form in the middle
  this.setVisible(true); // users can see form
  this.setResizable(false); //users can't resize the form
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }


 private class ListenForButton implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        String compareA = oppA.getText();
        String compareB = oppB.getText();


        if (e.getSource() == newthread)
        {   
            System.out.println(compareA + "\n" + compareB);

            for(int j = 0; j < array.length; j++)
            {
                System.out.println(array[j]);

                if(!compareA.equals(array[j]))

                {
                    JOptionPane.showMessageDialog(null, compareA + " doesn't exist!", "Error Message", JOptionPane.ERROR_MESSAGE);
                    oppA.requestFocus();
                    break;
                }

                if (!compareB.equals(array[j]))
                {
                    JOptionPane.showMessageDialog(null, compareB + " doesn't exist!", "Error Message", JOptionPane.ERROR_MESSAGE);
                    oppB.requestFocus();
                    break;
                }

                else 
                {
                    JOptionPane.showMessageDialog(null, "New thread created successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
                    break;
                }
            }
        }

        else if (e.getSource() == previewpost)
        {
            System.exit(0);
        }
    }





 }




public static void main(String[] args) {

   BattleOptionsPart1 battle = new BattleOptionsPart1();


}

}

最佳答案

在循环中的每个可能的选项中,您使用 break,这会立即离开循环。如果删除这些语句,您将处理数组中的每个对象。

如果你想检查是否有匹配,你需要遍历每个元素,并在遍历整个数组后进行处理。以下是 int 类型数组的示例:

boolean contains = false;
for (int i = 0; i < arr.length; i++)
{
    if (arr[i] == searchKey)
    {
        contains = true;
        break;
    }
}

关于java - java中for循环数组只处理一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16366410/

相关文章:

javascript - 遍历 FileList 声明

javascript - 为什么javascript `for`循环会快速静音/取消静音视频

javascript - 如何使用 for 循环遍历未知值

java - 循环if语句的问题

java - 我可以查出 java 程序是使用 java 还是 javaw 启动的

java程序有条件地从文件中读取行

java - Rsync 命令在 Java 中不起作用

java - 如何修复 Android.support.v7.widget.CardView 类膨胀错误

javascript - 如何在数组中一个一个地添加值

arrays - PowerShell执行速度的提高