2013-05-03 76 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」,則在第一個數組元素之後的命令爲