如何在Java中创建直方图 [英] how to create a histogram in java

查看:268
本文介绍了如何在Java中创建直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/13105765/how-to-convert-numbers-into-symbols-in-java-for-example-instead-of-2-to-or\">how将数字转换成Java符号?例如,而不是2 **,或3 ***等

我怎么能转换成数字直方图?直方图应根据该值多少次轧制显示从2-12的条形图。目前我的输出就像是第二张图,但想看起来像第一个。谢谢。
在这里输入的形象描述

How can I convert numbers into a histogram ? The histogram should display the bar graphs from 2-12 based upon how many times that value was rolled. currently my output is like the second pic but is suppose to look like the first one. thanks.

    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");




}

}

推荐答案

下面是$ C $的一些各个位c可以做到这一点。

Here are some various bits of code you can use to accomplish this.

创建阵列

int[] histogram = new int[13];

递增阵列中的位置

Increment a position in the array

histogram[id]++;

打印直方图

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

下面是一些辅助功能,以及

Here are some helper functions as well.

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

根据需要

code应该修改。

Code should be modified as needed.

这篇关于如何在Java中创建直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆