0

我的代码不会按照我想要的方式执行。我在整个 actionPerformed 类中放置了一些println语句,我认为它卡在了 foe 循环的末尾,因为它只通过 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

4 回答 4

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

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

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

于 2013-07-18T01:46:09.153 回答
4

问题出在这一行

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

你没有增加计数器。

for(int counter = 1; counter < 100_100; counter++){
//
}
于 2013-07-18T01:46:49.623 回答
2

改变

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

for(int counter = 1; counter < 100;counter++)
于 2013-07-18T01:46:55.120 回答
0

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

public int nextInt(int n)

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

因此,您需要 putint num = rand.nextInt(2);以获取01之间的值。

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

于 2013-07-18T02:10:23.443 回答