arrays - Java: Looping through and printing based on rounding -


i having trouble figuring out how display rounded number of asterisks based on corresponding (numbers on right of output) count.

i attempting use 1 asterisk represent 100 asterisks. however, when number such roll #17 being 417, want print 4 asterisks, not five; same rounding up. tried using math.round() unsuccessful.

i'd appreciate help.

my code:

public class histogram {      public static void main(string[] args) {         // todo auto-generated method stub          int numroles = 30000;         int[] amountroles = new int[19]; // amountroles holds array          (int = 3; < 7; i++)          amountroles[i] = 0; // set 0         {             (int = 0; < numroles; i++)              {                 int die1 = (int)(math.random()*6+1);                 int die2 = (int)(math.random()*6+1);                 int die3 = (int)(math.random()*6+1);                 amountroles[die1+die2+die3]++; // increments             }             system.out.print("the die rolled " + numroles + " times, 6 value's counts are:");             (int = 3; < 7; i++)             {                 system.out.println(); // line holder             }         }         (int = 3; < amountroles.length; i++) // iterates through amountroles         {             system.out.print("[" + + "]" + "  ");             for(int j = 0; j < amountroles[i]; j++) // loop through amountroles[i]             {                 if (math.round(j) % 100 == 0)                 {                     system.out.print("" + "*");                 }             }             system.out.println(" "  + amountroles[i]);         }     } } 

my output:

[3]     ** 139 [4]     **** 389 [5]     ********* 826 [6]     ************** 1366 [7]     ********************* 2082 [8]     ****************************** 2973 [9]     *********************************** 3440 [10]    *************************************** 3859 [11]    ************************************** 3742 [12]    *********************************** 3482 [13]    ****************************** 2918 [14]    ******************** 1996 [15]    ************** 1341 [16]    ********* 865 [17]    ***** 417 [18]    ** 165 

here part print each line:

system.out.print("[" + + "]" + "  "); for(int j = 0; j < amountroles[i]; j++) // loop through amountroles[i] {     if (math.round(j) % 100 == 0)     {         system.out.print("" + "*");     } } system.out.println(" "  + amountroles[i]); 

you looping hundreds of times. unnecessary , inefficent. use division number of *s print:

system.out.print("[" + + "]  "); int starstoprint = (int) math.round((double) amountroles[i] / 100.0); (int j = 0; j < starstoprint; j++) {     system.out.print("*"); } system.out.println(" "  + amountroles[i]); 

just information, reason original code broken because 0 % 100 == 0, on first iteration of inner loop print "*".


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -