Java: Array asterisk printing for values -
i trying print asterisks based on number of times dices side rolled (all through array). coming across issue printing of dice side (i) before asterisks. also, i'm getting 2 zeroes out of , don't know they're coming from. i'd appreciate help.
my code:
public class histogram { public static void main(string[] args) { // todo auto-generated method stub int numroles = 100; int[] amountroles = new int[7]; // amountroles holds array (int = 1; < 7; i++) amountroles[i] = 0; // set 0 { (int = 0; < numroles; i++) { int die1 = (int)(math.random()*6+1); amountroles[die1]++; // increments } system.out.println("the die rolled " + numroles + " times, 6 value's counts are:"); (int = 1; < 7; i++) { system.out.print("side " + + " rolled " + amountroles[i]+ " times out of " + numroles + "."); // prints each sides value (i) alongside how many times rolled (amountroles[i]). system.out.println(); // formatting line } } (int = 0; < amountroles.length; i++) // iterates through amountroles { for(int j = 0; j < amountroles[i]; j++) // loop through amountroles[i] { system.out.print("" + "*"); } system.out.println(i + " " + amountroles[i]); } } }
my output:
the die rolled 100 times, 6 value's counts are: side 1 rolled 11 times out of 100. side 2 rolled 19 times out of 100. side 3 rolled 19 times out of 100. side 4 rolled 17 times out of 100. side 5 rolled 16 times out of 100. side 6 rolled 18 times out of 100. 0 0 (where these zeroes coming from?) ***********1 11 *******************2 19 *******************3 19 *****************4 17 ****************5 16 ******************6 18
an example output aiming for:
[1] ******************* 19 [2] ************ 12 [3] ********************* 21 [4] ******************** 20 [5] ************* 13 [6] *************** 15
only answering question no other problems see code, getting 0 0
int = 0
. change int = 1
, no more 0 0
!
to values print way need, need simple move want print before *
above loop. in case [i]
, , move amount each roll below *
loop. code below. should fix stated problems.
for (int = 1; < amountroles.length; i++) // iterates through amountroles { system.out.print("[" + + "]"); for(int j = 0; j < amountroles[i]; j++) // loop through amountroles[i] { system.out.print("" + "*"); } system.out.println(" " + amountroles[i]); }
Comments
Post a Comment