简体   繁体   中英

Random Digit Generator Help (Java)

I have written a code that is generating random numbers based on user input. For example, if the user says they want the program to generate 3 random digits for them, and they want it to do it 3 times, the output will look like this:

1 4 5

2 6 5

3 4 5

The code that I've written would display this as:

1

4

5

2

6

5

3

4

5

How do I make them display as 3 numbers wide? Here's my code:

 if (gameType == 3)

    //for loop to generate random numbers     
    for (int i = 1; i <=numGames; i = i + 1){

        for(int j = 1; j <= numGames * 3; j = j + 1 ){
            ranNums = randomGenerator.nextInt(9);

            //Print random numbers to user
            System.out.println(ranNums);

As always, thank you! - I added to the post to help you understand more of what I need. Please let me know if you require more info to help me!

Generally loop should look like:

       for (int i = 1; i <= numGames; i = i + 1) {

            for (int j = 1; j<= gameType; j = j + 1) {
                int ranNums = randomGenerator.nextInt(9);
                System.out.print(ranNums + " ");
            }
            System.out.println();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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