1

I have this code that prints out the histogram of the rolls of some dice. my problem is that i want to print the first histogram next to the second one currently my output looks like this enter image description here but i want to put the stars on the right side of the number of times rolled like: 2 : 3 times /// 3 : 1 times / and so on.

        public static void main(String[] args) {
    // TODO code application logic here
    System.out.print("Please enter how many times to roll the dice: ");
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int [] rolls = new int[n];


    Random r1 = new Random();
    Random r2 = new Random();

    int dice1;
    int dice2;
    int [] t = new int [13];


    for (int roll=0; roll < rolls.length; roll++)
    {
         dice1 = r1.nextInt(6)+1;
         dice2 = r2.nextInt(6)+1;

         System.out.println(roll + " : I rolled a " + dice1 + " and a " + dice2);

         int sum;
         sum = dice1 + dice2;

         if (sum == 2)
             t[0]++;
         if (sum == 3)
             t[1]++;
         if (sum == 4)
             t[2]++;
         if (sum == 5)
             t[3]++;
         if (sum == 6)
             t[4]++;
         if (sum == 7)
             t[5]++;
         if (sum == 8)
             t[6]++;
         if (sum == 9)
             t[7]++;
         if (sum == 10)
             t[8]++;
         if (sum == 11)
             t[9]++;
         if (sum == 12)
             t[10]++;

    }   

System.out.println("Histogram of rolls:" ); 
    String star ="*";
    int [] h= {t[0], t[1],t[2], t[3],t[4], t[5], t[6], t[7],t[8], t[9],t[10]};
     for (int i=0; i <h.length; i++)
             {
        for(int j = 0; j < h[i]; j++) 
           System.out.print( star);
        System.out.println();
    }



    System.out.println("Histogram of rolls:" );

    System.out.println( "2 : " + t[0] + " times");
    System.out.println("3 : " + t[1] + " times");
    System.out.println("4 : " + t[2] + " times");
    System.out.println("5 : " + t[3] + " times");
    System.out.println("6 : " + t[4] + " times");
    System.out.println("7 : " + t[5] + " times");
    System.out.println("8 : " + t[6] + " times");
    System.out.println("9 : " + t[7] + " times");
    System.out.println("10 : " + t[8] + " times");
    System.out.println("11 : " + t[9] + " times");
    System.out.println("12 : " + t[10] + " times");

}

}

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
UnPatoCuacCuac
  • 315
  • 2
  • 7
  • 18

2 Answers2

1

Change the last part of your code to:

    ...
    System.out.println("Histogram of rolls:" ); 
    String star ="*";

    // No point in using "int [] h"

    for (int i=0; i < t.length; i++) {

        // Placing the logic for printing the text inside the loop
        // is how you use arrays
        System.out.print( (i+2) + " : " + t[0] + " times");
        for(int j = 0; j < t[i]; j++) {
           System.out.print(star);
        }
        System.out.println();
    }
}

More importantly, you may be missing a key advantage of using arrays: you should use loops whenever with arrays whenever possible instead of hardcoding sequential logic. For instance, that big cluster of if statements:

     if (sum == 2)
         t[0]++;
     if (sum == 3)
         t[1]++;
     if (sum == 4)
         t[2]++;
     if (sum == 5)
         t[3]++;
     if (sum == 6)
         t[4]++;
     if (sum == 7)
         t[5]++;
     if (sum == 8)
         t[6]++;
     if (sum == 9)
         t[7]++;
     if (sum == 10)
         t[8]++;
     if (sum == 11)
         t[9]++;
     if (sum == 12)
         t[10]++;

can be simply reduced to:

    t[sum-2]++;
sampson-chen
  • 45,805
  • 12
  • 84
  • 81
1

Suggestions:

1) You don't need array h[] at all

2) You need to print each line of your histogram ("***") and each line of your "Histogram number of rolls" together, inside the same loop

3) I would create a variable, "String asterisks = "************"; and print the correct number of asterisks with a subString.() copy, instead of a "for (...)" loop.

PS:

String.subString() is what I tried to suggest in your other question

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190