【问题标题】:how to create a histogram in java [duplicate]如何在java中创建直方图[重复]
【发布时间】:2012-10-17 21:33:48
【问题描述】:

可能重复:
how to convert numbers into symbols in java? for example instead of 2 to **, or 3 to *** etc.

如何将数字转换为直方图?直方图应根据滚动该值的次数显示 2-12 的条形图。目前我的输出就像第二张图片,但假设看起来像第一张。谢谢。

    public static void main(String[] args) {
    // TODO code application logic here
    System.out.print("Please enter how many times you want to roll two 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 two = 0;
    int three = 0;
    int four = 0;
    int five = 0;
     int six = 0;
    int seven = 0;
    int eight = 0;
    int nine = 0;
     int ten = 0;
    int eleven = 0;
    int twelve = 0;

    for (int roll=0; roll < rolls.length; roll++)
    {
         dice2 = r2.nextInt(6)+1;
         dice1 = r1.nextInt(6)+1;
         System.out.println(roll + " The first dice rolled a " + dice1 + " the second dice rolled a " + dice2);

         int sum;
         sum = dice1 + dice2;

         if (sum == 2)
             two++;
         if (sum == 3)
             three++;
         if (sum == 4)
             four++;
         if (sum == 5)
             five++;
         if (sum == 6)
             six++;
         if (sum == 7)
             seven++;
         if (sum == 8)
             eight++;
         if (sum == 9)
             nine++;
         if (sum == 10)
             ten++;
         if (sum == 11)
             eleven++;
         if (sum == 12)
             twelve++;

    }
    System.out.println("Histogram of rolls:" );  
    System.out.println("2 occurred " + two + " times");
    System.out.println("3 occurred " + three + " times");
    System.out.println("4 occurred " + four + " times");
    System.out.println("5 occurred " + five + " times");
    System.out.println("6 occurred " + six + " times");
    System.out.println("7 occurred " + seven + " times");
    System.out.println("8 occurred " + eight + " times");
    System.out.println("9 occurred " + nine + " times");
    System.out.println("10 occurred " + ten + " times");
    System.out.println("11 occurred " + eleven + " times");
    System.out.println("12 occurred " + twelve + " times");




}

}

【问题讨论】:

  • 我假设 #/asterisks 是 #/occurrences (在您的示例中,3: *****)。如果是这样,只需 1) 定义一个长度为 12 个星号的字符串,以及 2) 为直方图的每一行获取长度为 0..12 的 substring()。 PS:我会使用一个 int[] 数组而不是十二个单独的变量“一”,“二”,...
  • 已经问过这个问题,有 75 个代表。你可以开始赏金了。

标签: java arrays histogram


【解决方案1】:

这里有一些您可以用来完成此任务的各种代码。

创建数组

int[] histogram = new int[13];

在数组中增加一个位置

histogram[id]++;

打印直方图

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

这里还有一些辅助函数。

private void printHistogram(int[] array) {
     for (int range = 0; range < array.length; range++) {
        String label = range + " : ";
        System.out.println(label + convertToStars(array[range]));
    }
}

private String convertToStars(int num) {
    StringBuilder builder = new StringBuilder();
    for (int j = 0; j < num; j++) {
        builder.append('*');
    }
    return builder.toString();
}

应根据需要修改代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2011-06-21
    • 2018-04-16
    • 2016-02-21
    • 2023-03-11
    • 2015-08-26
    相关资源
    最近更新 更多