我使用shuffle()时出现java错误,但是当我使用randomNum()时却没有错误

时间:2014-06-27 18:42:37

标签: java

我的彩票游戏正在运转,但我需要改变计算机生成中奖号码的方式。起初我生成了一个随机数组,但我得到重复。为了解决这个问题,我改用了一个30个数字的数组并使用数组中的前六个数字。类似于在抽奖中下降的前6个球是如何使用的。当我运行该程序时,它似乎在compareResults()崩溃。在itemStateChanged(ItemEvent e)我呼叫randomNum()shuffle()。我不确定为什么它不会比较结果,因为我最终得到randNum[]无论如何  我使用什么方法来生成它。

        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 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;
        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);
    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);
    userPickedNumbers = new int[maxNumber];
}

public void itemStateChanged(ItemEvent e) {
    /*
     * actions taken 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[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 taken when the user has chosen 6 JCheckboxes
     */
    if (counter == maxNumber) {
        System.out.println("the picks have maxxed out");
        JCheckboxSetVisibleFalse();
        randNumber();
        //shuffle();
        compareResults();
    }
}

/*
 * creates an array of random lottery numbers
 * array of 30 numbers is created then shuffled. The 
 * First 6 elements are place in the 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));
            }




    public void shuffle(){
    Integer[] randomNumTemp = new Integer[LOTTERY_NUMBER_MAX];
    for (int i = 0; i < LOTTERY_NUMBER_MAX; i++) {
        randomNumTemp[i] = i+1;
    }
    //shuffle numbers
   Collections.shuffle(Arrays.asList(randomNumTemp));
   //add first 6 numbers to the randomNumTemp to randNum array
    Integer[] randomNum = new Integer[maxNumber];

    for (int i = 0; i < randomNum.length; i++) {
        randomNum[i] = randomNumTemp[i];
    }
}

/*
 * 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 < maxNumber - 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 value of j is" + j);
            System.out.println("the value of i is" + i);

        } else if (randomNum[j] == maxNumber
                && userPickedNumbers[i] == randomNum[j]) {
            j = 0;
            i++;
            matchedNumbers++;
        }

    } while (i < maxNumber);

}

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);
}

}

1 个答案:

答案 0 :(得分:1)

您没有将新的随机数混洗数组保存回临时数组中。我不认为Collections.shuffle使用.asList方法会做到这一点。只需将其保存回变量即可。将来,请使用调试器!逐行逐步查看该方法,以找到结果不是您想要的结果。