java - Eclipse Window Builder Monty hall gui -
my program set test monty hall problem 10000 times , set text boxes number of rounds (10000) , number of wins, doesnt take second text box , set text when method called, can tell me why?
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 random door other door1 or door2 */ private static int chooseanotherdoor(int door1, int door2) { int result; result = gen.nextint(3); while (result == door1 || result == door2); return result; } private jframe frame; private jtextfield textfield; /** * launch 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 application. */ public montyhallredo() { initialize(); } /** * initialize contents of 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; (int = 0; < rounds; i++) { int prize = gen.nextint(3); int userchoice1 = gen.nextint(3); // host opens door other user's choice without prize int hostchoice = chooseanotherdoor(prize, userchoice1); // user switches int userchoice2 = chooseanotherdoor(userchoice1, hostchoice); if (userchoice2 == prize) wins++; textfield_1.settext(string.format("%.4f", wins)); } } }
calculate()
getting called in initialize()
before gui objects created.
textfield_1.settext(string.format("%.4f", wins));
when above line called, textfield_1
not valid object.
Comments
Post a Comment