Würfel mit Statistik

Dan2306

Mitglied
Hallo Leute,

ich möchte einen Würfel programmieren und dazu eine Statistik ausgeben lassen. Die Programmierung des Würfels ist gelungen, er gibt auch den Wert des jeweiligen Wurfes aus. Problem ist jetzt jedoch meine Methode 'testeWuerfel', die zählen soll, wie oft die jeweilige Zahl gewürfelt wurde.

Hilfe wäre super,

vielen Dank!

Dan

Hier mein Quellcode:


Java:
public class Wuerfel{

private int wert;
private int zahl;
private int anzahlEins;
private int anzahlZwei;
private int anzahlDrei;
private int anzahlVier;
private int anzahlFuenf;
private int anzahlSechs;

public Wuerfel(){

anzahlEins = 0;
anzahlZwei = 0;
anzahlDrei = 0;
anzahlVier = 0;
anzahlFuenf = 0;
anzahlSechs = 0;

}

public void wuerfeln(){
wert=(int)(Math.random()*6+1);
}

public int getWert(){
return wert;
}

public void testeWuerfel(int anzahl)
{
for (int i=1; i <= anzahl; ++i);

wert=(int)(Math.random()*6+1);

if (wert = 1) {
    anzahlEins = anzahlEins + 1;
}
if (wert = 2) {
    anzahlZwei = anzahlZwei + 1;
}
if (wert = 3) {
    anzahlDrei = anzahlDrei + 1;
}
if (wert = 4) {
    anzahlVier = anzahlVier + 1;
}
if (wert = 5) {
    anzahlFuenf = anzahlFuenf + 1;
}
if (wert = 6) {
    anzahlSechs = anzahlSechs + 1;
}

}
// Out.println (+ anzahlEins +"-mal" "Augenzahl 1" );
// Out.println (+ anzahlZwei +"-mal" "Augenzahl 2");
// Out.println (+ anzahlDrei +"-mal" "Augenzahl 3");
// Out.println (+ anzahlVier +"-mal" "Augenzahl 4");
// Out.println (+ anzahlFuenf +"-mal" "Augenzahl 5");
// Out.println (+ anzahlSechs +"-mal" "Augenzahl 6");
}
 

Nicer

Bekanntes Mitglied
Java:
public void testeWuerfel(int anzahl)
{
for (int i=1; i <= anzahl; ++i);
 
wert=(int)(Math.random()*6+1);
 
if (wert = 1) {
    anzahlEins = anzahlEins + 1;
}
if (wert = 2) {
    anzahlZwei = anzahlZwei + 1;
}
if (wert = 3) {
    anzahlDrei = anzahlDrei + 1;
}
if (wert = 4) {
    anzahlVier = anzahlVier + 1;
}
if (wert = 5) {
    anzahlFuenf = anzahlFuenf + 1;
}
if (wert = 6) {
    anzahlSechs = anzahlSechs + 1;
}
 
}

da hastu die methode doch :bahnhof:
 
S

SlaterB

Gast
if (wert = 6) {

vs

if (wert == 6) {

-----

übrigens
anzahlSechs = anzahlSechs + 1;

==

anzahlSechs++;
 

Dan2306

Mitglied
ja, nur funktioniert es nicht.

In der Zeile
Java:
if (wert = 1) {

gibt mir BlueJ beim kompilieren den Fehler:

"incompatible types - found int bur expected boolean"

an.
 
S

SlaterB

Gast
siehe meine Antwort zuvor,
wenn dich hier gar der Compiler drauf hinweist, dann ist das doch einfach,
bei passenden boolean bekommt man nichtmal eine Fehlermeldung..
 

Dan2306

Mitglied
Vielen Dank slater. Das wusste ich nicht.

Das Problem, das bleibt ist, dass nicht anzahl-mal gewürfelt wird, sondern nur 1mal. Das Zählen wiederum funktioniert.
 

pl4gu33

Top Contributor
Java:
public class Wuerfel{
 
private int wert;
private int zahl;
private int anzahlEins;
private int anzahlZwei;
private int anzahlDrei;
private int anzahlVier;
private int anzahlFuenf;
private int anzahlSechs;
 
public Wuerfel(){
 
anzahlEins = 0;
anzahlZwei = 0;
anzahlDrei = 0;
anzahlVier = 0;
anzahlFuenf = 0;
anzahlSechs = 0;
 
}
 
public void wuerfeln(){
wert=(int)(Math.random()*6+1);
}
 
public int getWert(){
return wert;
}
 
public void testeWuerfel(int anzahl){
	int wert;	
	for (int i=1; i <= anzahl; ++i){ 
		wert=(int)(Math.random()*6+1);
		System.out.println(wert);
		 
		if (wert == 1) {
		    anzahlEins = anzahlEins + 1;
		}
		if (wert == 2) {
		    anzahlZwei = anzahlZwei + 1;
		}
		if (wert == 3) {
		    anzahlDrei = anzahlDrei + 1;
		}
		if (wert == 4) {
		    anzahlVier = anzahlVier + 1;
		}
		if (wert == 5) {
		    anzahlFuenf = anzahlFuenf + 1;
		}
		if (wert == 6) {
		    anzahlSechs = anzahlSechs + 1;
		}
	}
	System.out.println (anzahlEins +"-mal"+ "Augenzahl 1" );
	System.out.println (anzahlZwei +"-mal"+ "Augenzahl 1" );
	System.out.println (anzahlDrei +"-mal"+ "Augenzahl 1" );
	System.out.println (anzahlVier +"-mal"+ "Augenzahl 1" );
	System.out.println (anzahlFuenf +"-mal"+ "Augenzahl 1" );
	System.out.println (anzahlSechs +"-mal"+ "Augenzahl 1" );
	
}

so funktioniert deine Würfelklasse richtig du hast "wert = 5" gehabt, dass ist eine Wertzuweisung "wert == 5" ist eine Abfrage ob welche ein boolean wahr oder falsch zurück gibt.... weiterhin musst du deine For Schleife um deinen ganzen Block ziehen so "for (int i=1; i <= anzahl; ++i);" hast du nur eine Schleife mit keinem Sinn
 
Zuletzt bearbeitet:
Ähnliche Java Themen
  Titel Forum Antworten Datum
T Programmierauftrag Würfel Java Basics - Anfänger-Themen 4
Elyt Würfel mit bildlicher Anzeige Java Basics - Anfänger-Themen 2
x-tshainge Schleife für ein Würfel Programm Java Basics - Anfänger-Themen 2
T opengl Dreick in einen Würfel umwandeln Java Basics - Anfänger-Themen 0
A Würfel Java Basics - Anfänger-Themen 8
J BlueJ Liste Würfel Projekt Java Basics - Anfänger-Themen 2
W Gui Würfel. String Problem. Java Basics - Anfänger-Themen 7
U Erste Schritte Leidiges Anfängerthema: Würfel Java Basics - Anfänger-Themen 5
P Würfel Pasch Java Basics - Anfänger-Themen 24
M OOP Würfel Problem Java Basics - Anfänger-Themen 11
S Drehen RGB Würfel Java Basics - Anfänger-Themen 18
D Wahrscheinlichkeiten bei Würfel ändern Java Basics - Anfänger-Themen 7
C Würfel-Programm Java Basics - Anfänger-Themen 12
J 3d würfel Java Basics - Anfänger-Themen 13
L würfel zeichnen Java Basics - Anfänger-Themen 1
M Würfel spiel Java Basics - Anfänger-Themen 4
W Würfel ! Java Basics - Anfänger-Themen 15
BinaryLogic Datentypen Statistik Interface - untersch. Implementierung Java Basics - Anfänger-Themen 5
K Statistik aus ArrayList Java Basics - Anfänger-Themen 2
S Java Programm Statistik Wuerfeln Java Basics - Anfänger-Themen 19
A Formel 1 Statistik Programm Java Basics - Anfänger-Themen 2
S Wo gibts Statistik! Java Basics - Anfänger-Themen 3
W Statistik Java Basics - Anfänger-Themen 25

Ähnliche Java Themen

Neue Themen


Oben