Reputation: 339
I have a lottery game problem and the user picks 6 check boxes that generate the userPickedNumber and those numbers should be added to the array. As you can see when you run the code the numbers are printed out but when you look at the final array it prints out the the first 5 numbers as "0" and only the last one is entered properly. How did I manage this?
Also if i need to append this question with new code how do i do that?`
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 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 six number that match the 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;
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);
for (int i = 0; i < 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);
}
public void itemStateChanged(ItemEvent e) {
/*
* actions take as user checks the JCheckBoxes
*/
if (e.getStateChange() == ItemEvent.SELECTED && counter < maxNumber) {
temp = ((JCheckBox) e.getSource()).getText();
((JCheckBox) e.getSource()).setEnabled(false);
int intTemp = Integer.parseInt(temp);
userPickedNumbers = new int[maxNumber];
userPickedNumbers[counter] = intTemp;
counter++;
System.out.println("add to counter");
System.out.println("the .getText() returns "
+ ((JCheckBox) e.getSource()).getText());
System.out.println("the tempoaray int is " + intTemp);
System.out.println("the number of picks is " + counter);
System.out.println("**************************************");
}
/*
* actions take when the user has chosen 6 JCheckboxes
*/
if (counter == maxNumber) {
System.out.println("the picks have maxxed out");
JCheckboxSetVisibleFalse();
randNumber();
compareResults();
}
}
/*
* creates an array of random lottery number called randomNum[]
*/
public void randNumber() {
randomNum = new int[maxNumber];
for (int i = 0; i < maxNumber; i++) {
randomNum[i] = ((int) (Math.random() * 100) % lotteryNumbers.length + 1);
}
System.out.println("the randomNum array is "
+ Arrays.toString(randomNum));
}
/*
* compares the userPickedNumbers[] to the randomNum[]
*/
private void compareResults() {
System.out.println("the user picks are "
+ Arrays.toString(userPickedNumbers));
int i = 0;// user
int j = 0;// random
do {
if (userPickedNumbers[i] != randomNum[j]
&& j < randomNum.length - 1) {
System.out.println("the user picked " + userPickedNumbers[i]
+ " the random number is " + randomNum[j]);
System.out.println("wrong guess " + j);
System.out
.println("the location of the element in the fisrt array is "
+ i);
System.out
.println("the location of the element in the second array is "
+ 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 the first array is " + i);
j = 0;
i++;
} else if (userPickedNumbers[i] == randomNum[j]) {
matchedNumbers++;
i++;
j = 0;
System.out.println("the number of correct guesses are "
+ matchedNumbers);
System.out.println("the valur of j is" + j);
System.out.println("the valur of i is" + i);
} else if (randomNum[j] == randomNum.length
&& userPickedNumbers[i] == randomNum[j]) {
j = 0;
i++;
matchedNumbers++;
}
} while (i < userPickedNumbers.length);
}
private void JCheckboxSetVisibleFalse() {
for (int i = 0; i < lotteryNumbers.length; i++) {
checkBox[i].setVisible(false);
}
}
public static void main(String[] args) {
JLottery2 frame = new JLottery2();
frame.setVisible(true);
}
}
Upvotes: 0
Views: 91
Reputation: 347194
You keep initialising the userPickedNumbers
in the itemStateChanged
event handler, this means, your initialising its content to 0
each time the user clicks a check box
Initialise the array only at the start of the game
Upvotes: 1