java error when I use my shuffle() but not when I use my randomNum() -
i had lottery game working needed change way computer generated winning numbers. @ first generated array of random numbers getting duplicates. address switch shuffling array of 30 numbers , using first 6 in array. similar how first 6 balls drop in lottery ones used. when run program seems crash @ compareresults()
. in itemstatechanged(itemevent e)
have call randomnum()
, shuffle()
. not sure why not compare results since ending randnum[]
regardless of method use generate it.
import java.awt.borderlayout; import java.awt.color; import java.awt.container; import java.awt.flowlayout; import java.awt.font; import java.awt.gridlayout; import java.awt.event.itemevent; import java.awt.event.itemlistener; import java.util.arrays; import java.util.collections; import javax.swing.jbutton; import javax.swing.jcheckbox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; public class jlottery2 extends jframe implements itemlistener { private string[] lotterynumbers = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30" }; private jpanel jp1 = new jpanel(); private jpanel jp2 = new jpanel(); private jpanel jp3 = new jpanel(new gridlayout(3, 10, 5, 5)); private jlabel jl1 = new jlabel("the lottery game!!!!!"); private jlabel jl2 = new jlabel( "to play, pick 6 number match randomly selected numbers."); private flowlayout layout = new flowlayout(); private gridlayout gridbase = new gridlayout(3, 1, 5, 5); private gridlayout grid = new gridlayout(3, 10, 5, 5); private font heading = new font("palatino linotype", font.bold, 24); private font bodytext = new font("palatino linotype", font.bold, 14); private color color1 = new color(4, 217, 225); private color color2 = new color(4, 225, 129); private int maxnumber = 6; final int lottery_number_max = 30; private int counter = 0; private int[] randomnum; private int[] userpickednumbers; private int matchednumbers = 0; private jcheckbox checkbox[] = new jcheckbox[lotterynumbers.length]; private string temp; container con = getcontentpane(); public jlottery2() { super("the lottery game"); con.setlayout(gridbase); con.add(jp1); jp1.setlayout(layout); jp1.add(jl1); jl1.setfont(heading); jp1.setbackground(color1); con.add(jp2); jp2.setlayout(layout); jp2.add(jl2); jl2.setfont(bodytext); jp2.setbackground(color1); con.add(jp3); jp3.setlayout(grid); (int = 0; < lotterynumbers.length; i++) { // jcheckbox checkbox[] = new jcheckbox[lotterynumbers.length]; checkbox[i] = new jcheckbox(lotterynumbers[i]); jp3.add(checkbox[i]); jp3.setbackground(color2); checkbox[i].additemlistener(this); } setsize(500, 300); setdefaultcloseoperation(exit_on_close); userpickednumbers = new int[maxnumber]; } public void itemstatechanged(itemevent e) { /* * actions taken user checks jcheckboxes */ if (e.getstatechange() == itemevent.selected && counter < maxnumber) { temp = ((jcheckbox) e.getsource()).gettext(); ((jcheckbox) e.getsource()).setenabled(false); int inttemp = integer.parseint(temp); userpickednumbers[counter] = inttemp; counter++; system.out.println("add counter"); system.out.println("the .gettext() returns " + ((jcheckbox) e.getsource()).gettext()); system.out.println("the tempoaray int " + inttemp); system.out.println("the number of picks " + counter); system.out.println("**************************************"); } /* * actions taken when user has chosen 6 jcheckboxes */ if (counter == maxnumber) { system.out.println("the picks have maxxed out"); jcheckboxsetvisiblefalse(); randnumber(); //shuffle(); compareresults(); } } /* * creates array of random lottery numbers * array of 30 numbers created shuffled. * first 6 elements place in randomnum[] */ public void randnumber() { randomnum = new int[maxnumber]; (int = 0; < maxnumber; i++) { randomnum[i] = ((int) (math.random() * 100) % lotterynumbers.length + 1); } system.out.println("the randomnum array " + arrays.tostring(randomnum)); } public void shuffle(){ integer[] randomnumtemp = new integer[lottery_number_max]; (int = 0; < lottery_number_max; i++) { randomnumtemp[i] = i+1; } //shuffle numbers collections.shuffle(arrays.aslist(randomnumtemp)); //add first 6 numbers randomnumtemp randnum array integer[] randomnum = new integer[maxnumber]; (int = 0; < randomnum.length; i++) { randomnum[i] = randomnumtemp[i]; } } /* * compares userpickednumbers[] randomnum[] */ private void compareresults() { system.out.println("the user picks " + arrays.tostring(userpickednumbers)); int = 0;// user int j = 0;// random { if (userpickednumbers[i] != randomnum[j] && j < maxnumber - 1) { system.out.println("the user picked " + userpickednumbers[i] + " random number " + randomnum[j]); system.out.println("wrong guess " + j); system.out .println("the location of element in fisrt array " + i); system.out .println("the location of element in second array " + j); j++; } else if (userpickednumbers[i] != randomnum[j] && j == 5) { system.out.println(userpickednumbers[i] + randomnum[j]); system.out.println("one last wrong guess " + j); system.out.println("the location of first array " + i); j = 0; i++; } else if (userpickednumbers[i] == randomnum[j]) { matchednumbers++; i++; j = 0; system.out.println("the number of correct guesses " + matchednumbers); system.out.println("the value of j is" + j); system.out.println("the value of is" + i); } else if (randomnum[j] == maxnumber && userpickednumbers[i] == randomnum[j]) { j = 0; i++; matchednumbers++; } } while (i < maxnumber); } private void jcheckboxsetvisiblefalse() { (int = 0; < lotterynumbers.length; i++) { checkbox[i].setvisible(false); } } public static void main(string[] args) { jlottery2 frame = new jlottery2(); frame.setvisible(true); }
}
you're not saving new random number shuffled array temp array. don't think collections.shuffle
using .aslist
method in place. save variable. in future, use debugger! step through method line line find result isn't want it.
Comments
Post a Comment