2013-05-03 80 views
0

我不明白为什么每当我使用for-loop循环访问数组时,它只会产生一个控制台元素(第一个元素)?我很确定这是一个我正在查看的菜鸟错误,所以任何提示和建议都会有所帮助。for循环仅处理java中的一个元素?

我正在做一个有趣的程序,用于比较在文本字段中键入的两个字符串,如果它们不存在于数组中,它将产生相反的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(); 


} 

}

回答

6

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

如果你想检查是否有匹配,你需要通过每一个元素,并通过整个阵列后做你的处理。下面是int类型的数组的例子:

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

。 – 2013-05-03 19:46:38

+0

嗨,有没有办法来处理我的循环,并有效地评估我的if语句?我删除了休息,它没有工作。我用'继续'取代了休息,那没有奏效。我也尝试使用“返回”,这也没有奏效。 – user1801053 2013-05-03 21:30:11

+0

@ user1801053你想要发生什么? – nullptr 2013-05-03 21:31:36

1

你打破了循环。随着休息;如果你的意图是避免下一个“if”,使用“continue”而不是“break”,则在第一个数组元素之后的命令为