Java 中的直方图程序

Yashaswi Kothari 2021年6月29日
Java 中的直方图程序

直方图允许我们维护不同类别值的计数。我们也可以用图形来表示它们。

在本文中,我们将使用 Java 创建一个直方图,用于存储所掷骰子的总和。

为了检查这些值,我们将使用 if-else 阶梯。if-else 阶梯是一种比较一个元素和多个值的有效方法。我们在不同的整数变量中维护每个总和的计数。当匹配发生时,每个变量都会增加。

我们要求用户输入直方图数组的大小。该数组将代表每个骰子将被掷出的总次数。然后我们将初始化作为同时投掷的两个骰子的总和出现的数字,从 2 到 12。

用户输入两个骰子的值,我们将计算它们的总和。if-else 阶梯将比较不同可能性的总和,并在每次匹配发生时递增。

我们将在以下程序中实现所有这些。

 import java.io.*;
 class Main
 {
     public static String toStars(int number) {
    StringBuilder temp = new StringBuilder();
    for(int i=0;i<number;i++) {
        temp.append("*");
    }
    return temp.toString();
}
 public static void main(String[] args)throws IOException
 {
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 
    System.out.print("Total rolls for each dice?");
    
    int n = Integer.parseInt(br.readLine());
    int [] rolls = new int[n];
    int d1;
    int d2;
    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++)
    {
         System.out.println("First dice roll");
         d1 = Integer.parseInt(br.readLine());
         System.out.println("Second dice roll");
         d2 = Integer.parseInt(br.readLine());
         System.out.println(" The first dice rolled a " + d1 + " the second dice rolled a " + d2);

         int sum;
         sum = d1 + d2;

         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 " + toStars(two) + " times");
    System.out.println("3 occurred " + toStars(three) + " times");
    System.out.println("4 occurred " + toStars(four) + " times");
    System.out.println("5 occurred " + toStars(five) + " times");
    System.out.println("6 occurred " + toStars(six) + " times");
    System.out.println("7 occurred " + toStars(seven) + " times");
    System.out.println("8 occurred " + toStars(eight) + " times");
    System.out.println("9 occurred " + toStars(nine) + " times");
    System.out.println("10 occurred " + toStars(ten) + " times");
    System.out.println("11 occurred " + toStars(eleven) + " times");
    System.out.println("12 occurred " + toStars(twelve) + " times");

}


}

输出:

Total rolls for each dice?5

First dice roll

1

Second dice roll

2

 The first dice rolled a 1 the second dice rolled a 2

First dice roll

2

Second dice roll

1

 The first dice rolled a 2 the second dice rolled a 1

First dice roll

5

Second dice roll

4

 The first dice rolled a 5 the second dice rolled a 4

First dice roll

1

Second dice roll

1

 The first dice rolled a 1 the second dice rolled a 1

First dice roll

3

Second dice roll

1

 The first dice rolled a 3 the second dice rolled a 1

Histogram of rolls:

2 occurred * times

3 occurred ** times

4 occurred * times

5 occurred  times

6 occurred  times

7 occurred  times

8 occurred  times

9 occurred * times

10 occurred  times

11 occurred  times

12 occurred  times

请注意,为了显示最终输出,我们创建了一个单独的函数 toStars(),它将每种可能性的频率转换为星星的数量。这种方法在视觉上很吸引人,并且可以很好地表示直方图。