Java のヒストグラムプログラム
    
    
            Yashaswi Kothari
    2023年10月12日
    
    Java
    Java Histogram
    
 
ヒストグラムを使用すると、さまざまなカテゴリの値の数を維持できます。それらをグラフィカルに表現することもできます。
この記事では、Java を使用して、投げられたサイコロの合計を格納するヒストグラムを作成します。
値を確認するために、if-else ラダーを使用します。if-else ラダーは、1つの要素を複数の値と比較する効率的な方法です。各合計のカウントを異なる整数変数で維持します。各変数は、一致が発生するとインクリメントされます。
ヒストグラム配列のサイズをユーザーに入力してもらいます。この配列は、各サイコロが振られる合計回数を表します。次に、2 から 12 まで、同時にスローされた 2つのダイの合計として発生する数値を初期化します。
ユーザーが両方のサイコロの値を入力すると、それらの合計が計算されます。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() を作成することに注意してください。この方法は視覚的に魅力的であり、ヒストグラムを適切に表現できます。
        チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe