0

for ループを使用して配列を循環するたびに、コンソールに 1 つの要素 (最初の要素) しか生成されない理由がわかりません。私が見ているのは新人の間違いだと確信しているので、ヒントや提案があれば役立ちます。

テキスト フィールドに入力された 2 つの文字列を比較し、それらが配列に存在しない場合は逆に 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ループをすぐに終了する を使用します。これらのステートメントを削除すると、配列内の各オブジェクトが処理されます。

一致するかどうかを確認したい場合は、すべての要素を調べて、配列全体を調べた後に処理を行う必要があります。type の配列の例を次に示し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 に答える