如何多次打印数组?

时间:2015-07-02 03:39:41

标签: java arrays random

我的以下代码主要是别人的,我能够弄清楚如何打印正确的范围,但现在我想多次打印该范围(数组)。

    public class lottery {
    public static void main(String[] args) {

    int[] lottery = new int[6];
    int randomNum;

    for (int i = 0; i < 6; i++) {
        randomNum = (int) Math.ceil(Math.random() * 59); // Random number created here.
        for (int x = 0; x < i; x++) {
            if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
            {
                randomNum = (int) Math.ceil(Math.random() * 59);// If random number is same, another number generated.
                x = -1; // restart the loop
            }

        }
        lottery[i] = randomNum;
    }

    for (int i = 0; i < lottery.length; i++)
        System.out.print(lottery[i] + " ");

    }    
}

预期的输出是连续的六个整数:

12 52 46 22 7 33

遗憾的是,我无法找到与我的问题直接相关的任何内容。我是一个绝对的Java初学者,所以请保持温柔。

我想要的输出如下,其中每个x都是一个随机数。

    x x x x x x
    x x x x x x
    x x x x x x
    x x x x x x
    x x x x x x

从技术上讲,我希望最后一个数字是一个随机数,但是范围较小。我会再烧掉那座桥。

3 个答案:

答案 0 :(得分:1)

你的意思是这样吗?

var sameTextBox=[];
$('.test').each(function(){ //change this selector to class
     if($(this).attr('data-same')=="somevalue") //otherwise you can try 
     //if($(this).data('same')=="somevalue")
     {
          sameTextBox.push($(this));
     }
});

答案 1 :(得分:0)

for(int i = 0; i < lottery.length * howManyToPrint; i++){
    System.out.print(lottery[i] + " ");
}

答案 2 :(得分:0)

试试这样。您可以使用扫描仪输入您需要的次数。

 public class lottery {
    public static void main(String[] args) {
        int noTimes = 7;


        for (int j = 0; j < noTimes; j++) {
            int[] lottery = getRand();
            for (int i = 0; i < lottery.length; i++) {
                System.out.print(lottery[i] + " ");
            }
            System.out.println("\n");
        }
    }

    public int[] getRand() {
        int[] lottery = new int[6];
        int randomNum;
        for (int i = 0; i < 6; i++) {
            randomNum = (int) Math.ceil(Math.random() * 59); // Random number created here.
            for (int x = 0; x < i; x++) {
                if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
                {
                    randomNum = (int) Math.ceil(Math.random() * 59);// If random number is same, another number generated.
                    x = -1; // restart the loop
                }

            }
            lottery[i] = randomNum;
        }

        return lottery;
    }
}