如何在java中正确格式化或合并两个直方图?

时间:2012-10-29 01:46:50

标签: java format

我有这个代码打印出一些骰子的直方图。我的问题是,我想打印第二个旁边的第一个直方图,目前我的输出看起来像这个enter image description here,但我想把星星放在右侧的滚动次数如下: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");

}

}

2 个答案:

答案 0 :(得分:1)

将代码的最后一部分更改为:

    ...
    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();
    }
}

更重要的是,您可能缺少使用数组的关键优势:只要有可能,就应该使用循环,而不是硬编码顺序逻辑。例如,if语句的大集群:

     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]++;

可以简单地简化为:

    t[sum-2]++;

答案 1 :(得分:1)

建议:

1)你根本不需要数组h []

2)您需要在同一个循环内打印直方图(“***”)的每一行和“直方图卷数”的每一行

3)我会创建一个变量“String asterisks = "************";并使用subString。()副本打印正确数量的星号,而不是”for(...)“循环。

PS:

String.subString()是我在other question

中尝试建议的内容