How do I make this into a table output?

Cleo :

The problem is asking me to roll two dice and print their output individually in two separate columns and then create a third column for the sum of two rolls.

import java.util.Random;

public class DiceRolls {
    public static void main(String[] args) {
        System.out.println("Dice 1\tDice 2");
        Random ran = new Random();

        int numberOne;
        for (int x = 0; x < 7; x++) {
            numberOne = ran.nextInt(6) + 1;
            System.out.println(numberOne);
        }

        int numberTwo;
        for (int y = 0; y < 7; y++) {
            numberTwo = ran.nextInt(6) + 1;
            System.out.println("    " + numberTwo);
        }
    }
}
Nexevis :

I think you are thinking about this the wrong way and attempting to loop over all rolls of one die, THEN over the other die. If you instead attempt to roll both dice at the same time, then add them and print the output it makes things a lot simpler:

    //How many runs you want
    int numRuns = 7;

    for (int x = 0; x < numRuns; x++) {
        Random ran = new Random();
        int dieOne = ran.nextInt(6) + 1;
        int dieTwo = ran.nextInt(6) + 1;
        System.out.format("| Die 1:%3d| Die 2:%3d| Total:%3d|\n", dieOne, dieTwo, dieOne + dieTwo);
    }

This code will roll two dice 7 times and add them together. You can change the value of numRuns to change number of times it is run. You can then use System.out.format or String.format to create a formatted output.

What String.format or System.out.format does is basically use %3d to put the variables, for example dieOne, inside of the String in a formatted manner. This example of %3d can be broken down into 3 basic parts.

  • The 3 stands for number of characters to allow the variable to use, with the unused characters to be filled with extra spaces.

  • The d is the type of variable (in this case an int)

  • The % is used to signify that there is a variable in the String
    at that location.

So in summation: %3d is used to set the values of dieOne, dieTwo, and dieOne + dieTwo respectively into the String as an int each with a total of 3 characters.

In the edited example below, %4d, %4d, %5d have 4, 4, and 5 total characters that dieOne, dieTwo, and dieOne + dieTwo are set to respectively. The number of characters chosen were used to match the width of the headers of Die1, Die2, and Total.

EDIT: If you want it to look more like a table you can print it like this:

    //How many runs you want
    int numRuns = 7;

    System.out.println("-----------------");
    System.out.println("|Die1|Die2|Total|");
    System.out.println("-----------------");
    for (int x = 0; x < numRuns; x++) {
        Random ran = new Random();
        int dieOne = ran.nextInt(6) + 1;
        int dieTwo = ran.nextInt(6) + 1;
        System.out.format("|%4d|%4d|%5d|\n", dieOne, dieTwo, dieOne + dieTwo);
    }
    System.out.println("-----------------");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=144836&siteId=1