Java 直方图

Java 直方图,java,arrays,methods,graph,Java,Arrays,Methods,Graph,在我的程序中,我找到了平均值、模式、最大值和最小值。用户可以在1-50范围内添加任意数量的数字。现在,我想添加一个显示用户输入结果的直方图。直方图应该是这样的 1 - 5: **** 6 - 10: ****** 11 - 15: ** 16 - 20: ********** 21 - 25: *********** 26 - 31: ******** 31 - 35: **** 36 - 41: ******* 41 - 45: ************ 46 - 50: **********

在我的程序中,我找到了平均值、模式、最大值和最小值。用户可以在1-50范围内添加任意数量的数字。现在,我想添加一个显示用户输入结果的直方图。直方图应该是这样的

1 - 5: ****
6 - 10: ******
11 - 15: **
16 - 20: **********
21 - 25: ***********
26 - 31: ********
31 - 35: ****
36 - 41: *******
41 - 45: ************
46 - 50: *****************
我不知道如何编写代码。这是我的密码,请帮忙。导入java.util.Scanner

    int amount;
    System.out.println(" Enter the amount of numbers you would like to enter: ");
    amount = scan.nextInt();

    int [] arr = new int [amount];
    int outcome = 1;

    for (int i = 0; i < arr.length; i++){
        System.out.println("Enter a number 1 through 50");
        outcome = scan.nextInt();
        arr [i] = outcome;

    }

    System.out.println(" ");

    System.out.println( " The average is" ); 
    System.out.println(average(arr));
    System.out.println(" ");

    System.out.println( " The lowest value in the array is " ); 
    System.out.println(min(arr));
    System.out.println(" ");

    System.out.println( " The largest value in the array is " ); 
    System.out.println(max(arr));
    System.out.println(" ");

    System.out.println( " The most freuent number in the array is " ); 
    System.out.println(mode(arr));
    System.out.println(" ");

    System.out.println("");
    System.out.println("");
    System.out.println();graph (arr);

}





public static double average ( int [] arr) {

    double sum = 0;
    int value = arr.length;
    for ( int i = 0; i < arr.length; i++){
        sum += arr [i];
    }
    sum = sum / value;
    return sum;



}




public static int max(int[] arr) {

    int max = arr[0]; 
    for(int i = 1; i < arr.length; i++)
    {
        if(arr[i] > max)
        {
            max = arr[i];
        }
    }
    return max;
}





public static int min(int[] arr) {

    int min = arr[0];
    for(int i = 1; i < arr.length; i++)
    {
        if(arr[i] < min)
        {
            min = arr[i];
        }
    }
    return min;
}

public static int mode ( int[] arr) {
    int mode = arr[0];
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == mode) {
            mode=arr[i];
        }
    }
        return mode;
int金额;
System.out.println(“输入要输入的数字量:”);
金额=scan.nextInt();
int[]arr=新int[金额];
int结果=1;
对于(int i=0;i最大值)
{
max=arr[i];
}
}
返回最大值;
}
公共静态整数最小值(整数[]arr){
int min=arr[0];
对于(int i=1;i
}

公共静态无效图(int[]arr){
整数计数=0;
系统输出打印(“1-5:”);
对于(int i=0;i如果(arr[i]>0&&arr[i]5&&arr[i]一种方法是使用其键对应于数值范围的映射。您可以通过仅迭代一次值数组来轻松填充此映射:

public static void graph (int[] arr) {
    Map<Integer, Integer> map = new TreeMap<>();

    for (int i=0; i < arr.length; ++i) {
        int value = (arr[i] - 1) / 5;
        Integer val = map.get(value);
        map.put(value, val == null ? 1 : val.intValue() + 1);
    }

    // now iterate the map and print the histogram
    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        Integer key = entry.getKey();
        Integer value = entry.getValue();

        System.out.print((key*5 + 1) + " - " + (key*5 + 5) + ": ");
        for (int i=0; i < value; ++i) {
            System.out.print("*");
        }
        System.out.println();
    }
}
公共静态无效图(int[]arr){
Map Map=newtreemap();
对于(int i=0;i


直方图的每个部分都应该在自己的行上。@calvinjackson您的问题现在得到了很多反对票。您应该很高兴得到了任何帮助。在这种情况下,我已经给了您正确的工作代码。祝您好运!没有其他方法吗@TimBiegeleisen@calvinjackson(我假设这是一个家庭作业问题)我相信你在课堂上学到了一些适用于解决这个问题的东西。
public static void graph (int[] arr) {
    Map<Integer, Integer> map = new TreeMap<>();

    for (int i=0; i < arr.length; ++i) {
        int value = (arr[i] - 1) / 5;
        Integer val = map.get(value);
        map.put(value, val == null ? 1 : val.intValue() + 1);
    }

    // now iterate the map and print the histogram
    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        Integer key = entry.getKey();
        Integer value = entry.getValue();

        System.out.print((key*5 + 1) + " - " + (key*5 + 5) + ": ");
        for (int i=0; i < value; ++i) {
            System.out.print("*");
        }
        System.out.println();
    }
}