0

私のコードは思い通りに実行されません。actionPerformed クラス全体にいくつかのprintlnステートメントを配置しましたが、if ステートメントを 1 回しか通過しないため、foe ループの最後でスタックしていると思います。おそらく私が見落としている巨大なものがあるでしょう。何が悪いのか分かりますか?

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

0

また、int num = rand.nextInt(1);常にゼロを返します。Javaのドキュメントには次のように書かれているためです:

public int nextInt(int n)

0 (0 を含む)と指定された値( 0 を含まない)の間の一様分布の疑似乱数の int 値を返します。

したがって、 01int num = rand.nextInt(2);の間の値を取得するには put が必要です。

(パラメーターがないと、戻り値が01num = rand.nextInt();に制限されないため、 for ループ内の行も確認する必要があります)int n

于 2013-07-18T02:10:23.443 に答える