简体   繁体   English

如何多次打印数组?

[英]How do I print an array multiple times?

My following code is mostly somebody else's, I was able to figure out how to print the correct range, but now I want to print that range (array) multiple times. 我下面的代码大部分是别人的代码,我能够弄清楚如何打印正确的范围,但是现在我想多次打印该范围(数组)。

    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] + " ");

    }    
}

The output, as expected is six integers in a row: 预期的输出是连续六个整数:

12 52 46 22 7 33

I have unfortunately not been able to find anything directly relevant to my question. 不幸的是,我找不到与我的问题直接相关的任何东西。 I am an absolute Java beginner, so please be gentle. 我绝对是Java初学者,所以请保持谦虚。

The output I want is as follows, where each x is a random number. 我想要的输出如下,其中每个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

Technically, I'd like for the last number to be a random number, but a smaller range. 从技术上讲,我希望最后一个数字是随机数,但范围较小。 I'll burn that bridge another time. 我会再烧那座桥。

Do you mean something like this? 你的意思是这样吗?

for(int j = 0; j< numberOfTimeToPrint; j++)
   {
   for (int i = 0; i < lottery.length; i++)
       System.out.print(lottery[i] + " ");
   System.out.print("\n\n"); // 2 line feeds, 1 to terminate the line and another for a blank
   }
for(int i = 0; i < lottery.length * howManyToPrint; i++){
    System.out.print(lottery[i] + " ");
}

Try it like this. 这样尝试。 You can use scanner to input the number of times yu require. 您可以使用扫描仪输入您需要的次数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM