循环是否卡在某处?

时间:2013-07-18 01:43:03

标签: java loops for-loop

我的代码不会以我想要的方式执行。我在actionPerformed类中放了一些println语句,我认为它会在敌人循环结束时卡住,因为它只会通过if语句一次。我可能会忽视一些巨大的东西,你能看到什么是错的吗?

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;


    public class Frame extends JFrame implements ActionListener{

        public JPanel contentPane;
        public int yesNum, noNum;
        public JProgressBar progressBar;

    //  ######### CONFIG ##########
        public int genNum = 100_000;
    //Edit genNum to change number of tries


        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel("com.apple.laf.AquaLookAndFeel");
            } catch (Throwable e) {
                e.printStackTrace();
            }
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Frame frame = new Frame();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the frame.
         */
        public Frame() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 300, 195);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);

            JLabel lab = new JLabel("Click to generate " + genNum + " times");
            lab.setHorizontalAlignment(SwingConstants.CENTER);
            lab.setBounds(5, 5, 289, 16);
            contentPane.add(lab);

            JButton b = new JButton("Generate");
            b.setIcon(new ImageIcon("/Users/Colby/Desktop/checl.png"));
            b.setActionCommand("Generate");
            b.setBounds(80, 33, 139, 36);
            contentPane.add(b);

            JLabel yesLab = new JLabel("Yes Number: " + yesNum);
            yesLab.setBounds(22, 81, 117, 16);
            contentPane.add(yesLab);

            JLabel noLab = new JLabel("No Number: " + noNum);
            noLab.setBounds(22, 109, 117, 16);
            contentPane.add(noLab);

            progressBar = new JProgressBar();
            progressBar.setStringPainted(true);
            progressBar.setBounds(5, 141, 289, 20);
            progressBar.setMaximum(genNum);
            contentPane.add(progressBar);

            b.addActionListener(this);

        }

        public void actionPerformed(ActionEvent e){
            Random rand = new Random();
            int num = rand.nextInt(1);
            int yesCounter = 0;
            int noCounter = 0;

            for(int counter = 1; counter < 100_000;){
                if(num == 0){
                    System.out.println("testing yes");
                    yesCounter++;
                    System.out.println(counter+ ": Yes");
                    progressBar.setValue(counter);

                } else if(num == 1){
                    System.out.println("testing no");
                    noCounter++;
                    System.out.println(counter+ ": No");
                    progressBar.setValue(counter);
                }
                    num = rand.nextInt();
                }

            yesCounter = yesNum;
            noCounter = noNum;


        }
    }

4 个答案:

答案 0 :(得分:7)

for(int counter = 1; counter < 100_000;    )
                                       ^^^

您错过了迭代步骤,您应该在那里增加counter++counter)。

此外,像你这样的长期阻止任务不应该在EDT中执行,它应该拥有自己的Thread,因为你正在使用gui。

答案 1 :(得分:4)

问题出在这一行

for(int counter = 1; counter < 100_100;)

你没有增加柜台。

for(int counter = 1; counter < 100_100; counter++){
//
}

答案 2 :(得分:2)

更改

for(int counter = 1; counter < 100_000;)

for(int counter = 1; counter < 100;counter++)

答案 3 :(得分:0)

此外,int num = rand.nextInt(1);始终返回零。因为Java documentation说:

  

public int nextInt(int n)

     

返回 0之间的伪随机,均匀分布的int值   (包括)和指定值(不包括)

因此,您需要设置int num = rand.nextInt(2);以获取 0 1 之间的值。

(您还需要检查for循环中的行num = rand.nextInt();,因为没有int n参数,返回值不会限制为 0 1