0

我不明白为什么每当我使用 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();


}

}

4

2 回答 2

6

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

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

boolean contains = false;
for (int i = 0; i < arr.length; i++)
{
    if (arr[i] == searchKey)
    {
        contains = true;
        break;
    }
}
于 2013-05-03T19:44:42.817 回答
1

你打破了循环。随着休息; 第一个数组元素之后的命令

于 2013-05-03T19:46:41.523 回答