Skip to content

Instantly share code, notes, and snippets.

@ph3nx
Last active January 3, 2016 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ph3nx/8417465 to your computer and use it in GitHub Desktop.
Save ph3nx/8417465 to your computer and use it in GitHub Desktop.
Die Klasse Dice stellt einen Würfel dar. Bei der Erzeugung eines Objekts wird die Anzahl an Würfen eingegeben, die durchgeführt werden sollen. Mit Hilfe der Methode zeigeStatistik() erhält man eine Übersicht wie oft jede Zahl gewürfelt wurde sowie den Durchschnitt, der normal wäre.
public class Dice {
private int anzahl;
private int grenze = 6;
private int[] zaehler = new int[6];
Dice(int wuerfeAnzahl){
anzahl = wuerfeAnzahl;
for(int i=0; i<anzahl; i++){
zaehle(gibZufallszahl());
}
}
private int gibZufallszahl(){
return (int) (Math.random() * grenze);
}
private void zaehle(int wuerfelnummer){
zaehler[wuerfelnummer]++;
}
public void zeigeStatistik(){
for (int i=0; i<grenze; i++){
System.out.println("#"+(i+1)+" wurde "+zaehler[i]+"mal gewürfelt.");
}
System.out.println("Insgesamt wurde "+anzahl+"mal gewürfelt.");
System.out.println("Durchschnitt wäre: "+anzahl/grenze);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment