我有这段代码可以打印出一些骰子的直方图。我的问题是我想在第二个直方图旁边打印第一个直方图,目前我的输出看起来像这样,但我想把星星放在滚动次数的右侧:2:3 次 /// 3: 1次/以此类推。
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");
}
}