Histogrammprogramm in Java

Yashaswi Kothari 12 Oktober 2023
Histogrammprogramm in Java

Histogramme ermöglichen es uns, die Zählung verschiedener Kategorien von Werten zu verwalten. Wir können sie auch grafisch darstellen.

In diesem Artikel verwenden wir Java, um ein Histogramm zu erstellen, das die Summe der geworfenen Würfel speichert.

Um die Werte zu überprüfen, verwenden wir die if-else-Leiter. Die if-else-Leiter ist eine effiziente Möglichkeit, ein Element mit mehreren Werten zu vergleichen. Wir pflegen die Zählung jeder Summe in verschiedenen ganzzahligen Variablen. Jede Variable wird inkrementiert, wenn die Übereinstimmung auftritt.

Wir bitten den Benutzer, die Größe des Histogramm-Arrays einzugeben. Dieses Array gibt an, wie oft jeder Würfel insgesamt gewürfelt wird. Wir initialisieren dann die Zahlen, die als Summe von zwei gleichzeitig geworfenen Würfeln auftreten, beginnend von 2 bis 12.

Der Benutzer gibt den Wert für beide Würfel ein und wir berechnen ihre Summe. Die if-else-Leiter vergleicht die Summe mit verschiedenen Möglichkeiten und erhöht jedes Mal, wenn eine Übereinstimmung auftritt.

All dies werden wir im folgenden Programm umsetzen.

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

Ausgabe:

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

Beachten Sie, dass wir zur Anzeige der endgültigen Ausgabe eine separate Funktion toStars() erstellen, die die Häufigkeit für jede Möglichkeit in die Anzahl der Sterne umwandelt. Diese Methode ist optisch ansprechend und liefert eine gute Darstellung eines Histogramms.