私のプログラムは、モンティ ホール問題を 10000 回テストし、テキスト ボックスにラウンド数 (10000) と勝利数を設定するように設定されています。2 番目のテキスト ボックスを使用せず、メソッドが呼び出されたときにテキストを設定するだけで、誰か教えて?
package com.main.www;
import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MontyHallRedo {
public static final Random gen = new Random();
public static final int ROUNDS = 10000;
/** chooses a random door other than door1 or door2 */
private static int chooseAnotherDoor(int door1, int door2) {
int result;
do
result = gen.nextInt(3);
while (result == door1 || result == door2);
return result;
}
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MontyHallRedo window = new MontyHallRedo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
calculate();
}
String ROUNDS2 = String.valueOf(ROUNDS);
private JLabel lblRoundsWon;
private static JTextField textField_1;
/**
* Create the application.
*/
public MontyHallRedo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
calculate();
frame = new JFrame();
frame.setBounds(100, 100, 716, 507);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblRounds = new JLabel("Rounds Played:");
lblRounds.setBounds(10, 155, 98, 49);
frame.getContentPane().add(lblRounds);
textField = new JTextField();
textField.setBounds(94, 169, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField.setText(ROUNDS2);
textField.setEditable(false);
lblRoundsWon = new JLabel("Rounds Won:");
lblRoundsWon.setBounds(237, 155, 68, 49);
frame.getContentPane().add(lblRoundsWon);
textField_1 = new JTextField();
textField_1.setBounds(315, 169, 86, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_1.setEditable(false);
}
private static void calculate() {
int wins = 0;
for (int i = 0; i < ROUNDS; i++) {
int prize = gen.nextInt(3);
int userChoice1 = gen.nextInt(3);
// host opens door other than user's choice without prize
int hostChoice = chooseAnotherDoor(prize, userChoice1);
// user always switches
int userChoice2 = chooseAnotherDoor(userChoice1, hostChoice);
if (userChoice2 == prize)
wins++;
textField_1.setText(String.format("%.4f", wins));
}
}
}