Java 中的直方圖程式

Yashaswi Kothari 2023年10月12日
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(),它將每種可能性的頻率轉換為星星的數量。這種方法在視覺上很吸引人,並且可以很好地表示直方圖。