Problem bei meinem Programm : Lottozahlen

Status
Nicht offen für weitere Antworten.

JeromeM90

Aktives Mitglied
Hallo liebe Gemeinde,


ich habe ein Problem mit meinem Programm (Lottozahlen).


Hier ist mein Java-Code:

[highlight=Java]package lottozahlen;

/*
********************************************************************************
* Programmname: Lottozahlen *
* Datum: 7.04.2009 *
* Erstellt von: Jerome *
* *
* Beschreibung: *
* *
* Es sollen 6 Zahlen eingegeben werden und 6 automatisch erzeugt werden. *
* Dieses Programm vergleicht die 6 Zahlen und gibt die Übereinstimmungen aus. *
********************************************************************************
*/

import javax.swing.*;

public class Lottozahlen {

int eingabe (String dialogtext){
// Eingabe der 6 Tipps

final int NUM_ZAHL = 6;
final int ANZ_POTT = 49;

int[] Tipp = new int [NUM_ZAHL];
int[] Tipppott = new int [ANZ_POTT+1];

int H;
String sTipp = dialogtext;

for (int i = 0; i <= ANZ_POTT; i++)
Tipppott = 0;

for (int i = 0; i <= NUM_ZAHL; i++){

do{

Tipp = Integer.parseInt(JOptionPane.showInputDialog("Ihre " +(i+1)+ ". Tippzahl lautet: "));

} while (Tipp>49 || (Tipppott[Tipp]==1) || Tipp<1);

Tipppott[Tipp] = 1;

}

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if(Tipp < Tipp[j]){

H = Tipp;
Tipp = Tipp[j];
Tipp[j] = H;

}

}
}

for (int i = 0; i <= 5; i++)
sTipp = sTipp + Tipp + ". ";

int eingabe = Integer.parseInt(sTipp);
return eingabe;

}

int lottozahlen (String dialogtext){
// Lottozahlen werden generiert

final int NUM_ZAHL = 6;
final int ANZ_POTT = 49;

int[] Lottozahlen = new int [NUM_ZAHL];
int[] Lottopott = new int [ANZ_POTT+1];
int H;

String sLotto = dialogtext;

for (int i = 0; i <= ANZ_POTT; i++)
Lottopott = 0;

for (int i = 0; i <= NUM_ZAHL; i++){

do {

Lottozahlen = (int)(Math.random()*ANZ_POTT)+1;

} while (Lottopott[Lottozahlen] == 1);

Lottopott[Lottozahlen] = 1;

}

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if(Lottozahlen < Lottozahlen[j]){

H = Lottozahlen;
Lottozahlen = Lottozahlen[j];
Lottozahlen[j] = H;

}

}
}

for (int i = 0; i < NUM_ZAHL; i++)
sLotto = sLotto + Lottozahlen + ". ";

int lottozahlen = Integer.parseInt(sLotto);
return lottozahlen;

}

int treffer (int eingabe, int lottozahlen){
// Treffer werden gezaehlt

int trefferzaehler = 0;

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if (eingabe == lottozahlen)
trefferzaehler++;

}
}

return trefferzaehler;

}

void ausgabe (int lottozahlen, int eingabe, int treffer){
// Ausgabe von lottozahlen, eingabe & treffer

String gZahlen = Integer.toString(lottozahlen);
String tipp = Integer.toString(eingabe);
String trefferzaehler = Integer.toString(treffer);

JOptionPane.showMessageDialog(null, gZahlen);
JOptionPane.showMessageDialog(null, tipp);
JOptionPane.showMessageDialog(null, trefferzaehler);

}

public static void main(String[] args) {
// Methodenaufruf von eingabe, lottozahlen & treffer

Lottozahlen meinLotto = new Lottozahlen();
int eingabe = meinLotto.eingabe("Ihre Zahlen lauten : ");
int lottozahlen = meinLotto.lottozahlen("Folgende Zahlen wurden gezogen : ");
int treffer = meinLotto.treffer(eingabe, lottozahlen);
meinLotto.ausgabe(lottozahlen, eingabe, treffer);

}
}[/highlight]

Und hier ist folgende Fehlermeldung:

[highlight=Java]Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at lottozahlen.Lottozahlen.eingabe(Lottozahlen.java:39)
at lottozahlen.Lottozahlen.main(Lottozahlen.java:153)[/highlight]

Ich hoffe ihr könnt mir weiterhelfen.
Bitte verbessert meine Fehler (oder eher was ich ändern soll) und gibt mir keine anderen Codes.

DANKE!
 

Der Müde Joe

Top Contributor
final int NUM_ZAHL = 6;
...
int[] Tipp = new int [NUM_ZAHL];
...
for (int i = 0; i <= NUM_ZAHL; i++){

ein Array der Länge 6 geht von 0 bis 5

EDIT:
Allgemein empfiehlt es sich, wenn man über Arrays iteriert, eine dynamische
Variable zu benutzen ( i < array.length). Denn so hat man immer
die Länge des Arrays (auch wenn sie hier fix ist, im Sinne von immer 6)
EDIT2:
soll nicht heissen, das ein Array eine dynamische Länge hat ;-)
 
Zuletzt bearbeitet:

JeromeM90

Aktives Mitglied
Ah ok.

Aber mein Problem ist nur, wenn ich mein Programm ausführe, dann kommt ja "Ihre 1. Tippzahl lautet :" (usw.).
So und wenn er damit fertig ist wird das Programm abgebrochen.
 

Schandro

Top Contributor
und wie immer die üblichen Hinweise:

Bei Variablennamen schreibt man den ersten Buchstaben klein
nach for, while, do, switch oder if-Statements immer {...} schreiben, niemals die Klammern weglassen nur weil nur eine einzige Anweisung folgt.
 

Der Müde Joe

Top Contributor
>So und wenn er damit fertig ist wird das Programm abgebrochen.

Ein unsafte Art des Abbruchs...
Naja..halt beim letzen Schleifendurchgang.. wenn i = 6 und i <= NUM_ZAH (6=6)
dann Tipp[6] ..peng

java.lang.ArrayIndexOutOfBoundsException: 6
 

JeromeM90

Aktives Mitglied
Ok. Das werde ich mal machen. Danke schonmal. Ich habe es nur von meinem Lehrer gehört das man die {} bei einer Anweisung weglassen kann.
 

Der Müde Joe

Top Contributor
>Lehrer gehört das man die {} bei einer Anweisung weglassen kann.

und so was schimpft sich Lehrer...

Können ja, aber eine Fehlerquelle hoch 3....
 

JeromeM90

Aktives Mitglied
lol ^^

EDIT: Der Fehler ist immernoch da. Ich habe jetzt die Variablennamen kleingeschrieben und ich habe die Klammern gesetzt.

Hier mein (aktualisierter) Code:

[highlight=Java]package lottozahlen;

/*
********************************************************************************
* Programmname: Lottozahlen *
* Datum: 7.04.2009 *
* Erstellt von: Jerome *
* *
* Beschreibung: *
* *
* Es sollen 6 Zahlen eingegeben werden und 6 automatisch erzeugt werden. *
* Dieses Programm vergleicht die 6 Zahlen und gibt die Übereinstimmungen aus. *
********************************************************************************
*/

import javax.swing.*;

public class Lottozahlen {

int eingabe (String dialogtext){
// Eingabe der 6 Tipps

final int num_zahl = 5;
final int anz_pott = 49;

int[] tipp = new int [num_zahl];
int[] tipppott = new int [anz_pott+1];

int H;
String sTipp = dialogtext;

for (int i = 0; i <= anz_pott; i++){

tipppott = 0;

}

for (int i = 0; i <= num_zahl; i++){

do{

tipp = Integer.parseInt(JOptionPane.showInputDialog("Ihre " +(i+1)+ ". Tippzahl lautet: "));

} while (tipp>49 || (tipppott[tipp]==1) || tipp<1);

tipppott[tipp] = 1;

}

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if(tipp < tipp[j]){

H = tipp;
tipp = tipp[j];
tipp[j] = H;

}

}
}

for (int i = 0; i <= 5; i++){

sTipp = sTipp + tipp + ". ";

}

int eingabe = Integer.parseInt(sTipp);
return eingabe;

}

int lottozahlen (String dialogtext){
// Lottozahlen werden generiert

final int num_zahl = 6;
final int anz_pott = 49;

int[] lottozahlen = new int [num_zahl];
int[] lottopott = new int [anz_pott+1];
int H;

String sLotto = dialogtext;

for (int i = 0; i <= anz_pott; i++){

lottopott = 0;

}

for (int i = 0; i <= num_zahl; i++){

do {

lottozahlen = (int)(Math.random()*anz_pott)+1;

} while (lottopott[lottozahlen] == 1);

lottopott[lottozahlen] = 1;

}

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if(lottozahlen < lottozahlen[j]){

H = lottozahlen;
lottozahlen = lottozahlen[j];
lottozahlen[j] = H;

}

}
}

for (int i = 0; i < num_zahl; i++){

sLotto = sLotto + lottozahlen + ". ";

}

int zahlen = Integer.parseInt(sLotto);
return zahlen;

}

int treffer (int eingabe, int lottozahlen){
// Treffer werden gezaehlt

int trefferzaehler = 0;

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if (eingabe == lottozahlen)
trefferzaehler++;

}
}

return trefferzaehler;

}

void ausgabe (int lottozahlen, int eingabe, int treffer){
// Ausgabe von lottozahlen, eingabe & treffer

String gzahlen = Integer.toString(lottozahlen);
String tipp = Integer.toString(eingabe);
String trefferzaehler = Integer.toString(treffer);

JOptionPane.showMessageDialog(null, gzahlen);
JOptionPane.showMessageDialog(null, tipp);
JOptionPane.showMessageDialog(null, trefferzaehler);

}

public static void main(String[] args) {
// Methodenaufruf von eingabe, lottozahlen & treffer

Lottozahlen meinLotto = new Lottozahlen();
int eingabe = meinLotto.eingabe("Ihre Zahlen lauten : ");
int lottozahlen = meinLotto.lottozahlen("Folgende Zahlen wurden gezogen : ");
int treffer = meinLotto.treffer(eingabe, lottozahlen);
meinLotto.ausgabe(lottozahlen, eingabe, treffer);

}
}[/highlight]
 
Zuletzt bearbeitet:

schalentier

Gesperrter Benutzer
Die Exception kommt daher:
Code:
final int num_zahl = 5;
..
int[] tipp = new int [num_zahl];

Du muesstest aber schreiben:
Code:
final int num_zahl = 5;
..
int[] tipp = new int [num_zahl+1];

Generell is das aber irgendwie... unueblich. So macht man das ueblicherweise (hoffentlich hab ich nix verpeilt):
Code:
int numberOfTips = 6;
int[] tipp = new int [numberOfTips];

for( int i=0; i<numberOfTips; i++ ) {
   int currentTip;
   do{
      currentTip = Integer.parseInt(JOptionPane.showInputDialog("Ihre " +(i+1)+ ". Tippzahl lautet: "));
   } while (currentTip>49 || (tipppott[currentTip]==1) || currentTip<1);
   tipppott[currentTip] = 1;
}
 

Der Müde Joe

Top Contributor
und wenn wir schon dabei sind:
int[] lottozahlen = new int [num_zahl];
...
for (int i = 0; i <= num_zahl; i++){
lottozahlen =..

zb mit 3 (länge des Arrays)
i=0 ... 0 <= 3 lottozahlen[0] ...ok
i=1 ... 1 <= 3 lottozahlen[1] ...ok
i=2 ... 2 <= 3 lottozahlen[2] ...ok
i=3 ... 3 <= 3 lottozahlen[3] ...ArrayIndexOutOfBoundsException: 3

>So macht man das ueblicherweise

array.length, nicht eine Zahl...auch wenn sie die Länge des Arrays bestimmt
 

Matt

Aktives Mitglied
voll die hardcore antworten ^^ dabei springt er doch einfach über das array hinaus...
was die klammern angeht lass ich die auch hinund wieder weg - funktionieren tut es in 99,9% aller fälle xD

einfach erklärung für den herrn

in der Zeile 35 ist eine For-Schleife, welche das das Array durchlaufen tut - und diese For-schleife läuft soweit das sie über das array hinaus hüpft ^^

Einfach aus:

[highlight=Java] for (int i = 0; i <= NUM_ZAHL; i++){...}[/highlight]

das machen

[highlight=Java] for (int i = 0; i < NUM_ZAHL; i++){...}[/highlight]

(das <= wurde in ein < verwandelt :shock:)

Oo wenn es da nochmehr fehler gibt kA hab nur den nun gesucht xD
 

JeromeM90

Aktives Mitglied
Ich habe jetzt alle eure Probleme (außer array.lenght) ausprobiert und es kommen jetzt nochmehr Fehler xD

[highlight=Java]package lottozahlen;

/*
********************************************************************************
* Programmname: Lottozahlen *
* Datum: 7.04.2009 *
* Erstellt von: Jerome *
* *
* Beschreibung: *
* *
* Es sollen 6 Zahlen eingegeben werden und 6 automatisch erzeugt werden. *
* Dieses Programm vergleicht die 6 Zahlen und gibt die Übereinstimmungen aus. *
********************************************************************************
*/

import javax.swing.*;

public class Lottozahlen {

int eingabe (String dialogtext){
// Eingabe der 6 Tipps

final int num_zahl = 5;
final int anz_pott = 49;

int[] tipp = new int [num_zahl+1];
int[] tipppott = new int [anz_pott+1];

int H;
String sTipp = dialogtext;

for (int i = 0; i <= anz_pott; i++){

tipppott = 0;

}

for (int i = 0; i < num_zahl; i++){

do{

tipp = Integer.parseInt(JOptionPane.showInputDialog("Ihre " +(i+1)+ ". Tippzahl lautet: "));

} while (tipp>49 || (tipppott[tipp]==1) || tipp<1);

tipppott[tipp] = 1;

}

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if(tipp < tipp[j]){

H = tipp;
tipp = tipp[j];
tipp[j] = H;

}

}
}

for (int i = 0; i <= 5; i++){

sTipp = sTipp + tipp + ". ";

}

int eingabe = Integer.parseInt(sTipp);
return eingabe;

}

int lottozahlen (String dialogtext){
// Lottozahlen werden generiert

final int num_zahl = 5;
final int anz_pott = 49;

int[] lottozahlen = new int [num_zahl+1];
int[] lottopott = new int [anz_pott+1];
int H;

String sLotto = dialogtext;

for (int i = 0; i <= anz_pott; i++){

lottopott = 0;

}

for (int i = 0; i <= num_zahl; i++){

do {

lottozahlen = (int)(Math.random()*anz_pott)+1;

} while (lottopott[lottozahlen] == 1);

lottopott[lottozahlen] = 1;

}

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if(lottozahlen < lottozahlen[j]){

H = lottozahlen;
lottozahlen = lottozahlen[j];
lottozahlen[j] = H;

}

}
}

for (int i = 0; i < num_zahl; i++){

sLotto = sLotto + lottozahlen + ". ";

}

int zahlen = Integer.parseInt(sLotto);
return zahlen;

}

int treffer (int eingabe, int lottozahlen){
// Treffer werden gezaehlt

int trefferzaehler = 0;

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if (eingabe == lottozahlen)
trefferzaehler++;

}
}

return trefferzaehler;

}

void ausgabe (int lottozahlen, int eingabe, int treffer){
// Ausgabe von lottozahlen, eingabe & treffer

String gzahlen = Integer.toString(lottozahlen);
String tipp = Integer.toString(eingabe);
String trefferzaehler = Integer.toString(treffer);

JOptionPane.showMessageDialog(null, gzahlen);
JOptionPane.showMessageDialog(null, tipp);
JOptionPane.showMessageDialog(null, trefferzaehler);

}

public static void main(String[] args) {
// Methodenaufruf von eingabe, lottozahlen & treffer

Lottozahlen meinLotto = new Lottozahlen();
int eingabe = meinLotto.eingabe("Ihre Zahlen lauten : ");
int lottozahlen = meinLotto.lottozahlen("Folgende Zahlen wurden gezogen : ");
int treffer = meinLotto.treffer(eingabe, lottozahlen);
meinLotto.ausgabe(lottozahlen, eingabe, treffer);

}
}[/highlight]

Und hier meine Fehler bei Eclipse:

[highlight=Java]Exception in thread "main" java.lang.NumberFormatException: For input string: "Ihre Zahlen lauten : 1. 2. 3. 4. 5. 6. "
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at lottozahlen.Lottozahlen.eingabe(Lottozahlen.java:70)
at lottozahlen.Lottozahlen.main(Lottozahlen.java:165)
[/highlight]
 

Matt

Aktives Mitglied
Also Oo damit wir mal verstehen was du da gemacht hast xD

Das ist dein Aufruf:
[highlight=Java] int eingabe = meinLotto.eingabe("Ihre Zahlen lauten : ");
[/highlight]

Der aufruf der methode schaut so aus:
[highlight="Java"]int eingabe (String dialogtext)[/highlight]

An dieser Stelle übernimmst du den Parameter in einer Variable:
[highlight="Java"] String sTipp = dialogtext;
[/highlight]

Wir sehen es steht schon was drin xD und zwar "Ihre Zahlen lauten : "...

Unten hängst du dem string noch was an:
[highlight="Java"] for (int i = 0; i <= 5; i++){

sTipp = sTipp + tipp + ". ";

}
[/highlight]

Bis hierhin sollte alles passen Oo

dann kommt das:
[highlight="Java"]
int eingabe = Integer.parseInt(sTipp);
return eingabe;
[/highlight]

du machst aus deinem ganzen String ein Int und gibst ihn zurück - ich denke hier liegt das problem Oo vllt solltest du direkt die int-arrays zurückgeben bzw. weiter geben Oo und der ausgabe die extras überlassen

so könnte das aussehen:

[highlight=Java]package lottozahlen;

/*
********************************************************************************
* Programmname: Lottozahlen *
* Datum: 7.04.2009 *
* Erstellt von: Jerome *
* *
* Beschreibung: *
* *
* Es sollen 6 Zahlen eingegeben werden und 6 automatisch erzeugt werden. *
* Dieses Programm vergleicht die 6 Zahlen und gibt die Übereinstimmungen aus. *
********************************************************************************
*/

import javax.swing.*;

public class Lottozahlen {

int[] eingabe (){
// Eingabe der 6 Tipps

final int num_zahl = 5;
final int anz_pott = 49;

int[] tipp = new int [num_zahl+1];
int[] tipppott = new int [anz_pott+1];

int H;

for (int i = 0; i <= anz_pott; i++){

tipppott = 0;

}

for (int i = 0; i < num_zahl; i++){

do{

tipp = Integer.parseInt(JOptionPane.showInputDialog("Ihre " +(i+1)+ ". Tippzahl lautet: "));

} while (tipp>49 || (tipppott[tipp]==1) || tipp<1);

tipppott[tipp] = 1;

}

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if(tipp < tipp[j]){

H = tipp;
tipp = tipp[j];
tipp[j] = H;

}

}
}


return tipp;

}

int[] lottozahlen (){
// Lottozahlen werden generiert

final int num_zahl = 5;
final int anz_pott = 49;

int[] lottozahlen = new int [num_zahl+1];
int[] lottopott = new int [anz_pott+1];
int H;


for (int i = 0; i <= anz_pott; i++){

lottopott = 0;

}

for (int i = 0; i <= num_zahl; i++){

do {

lottozahlen = (int)(Math.random()*anz_pott)+1;

} while (lottopott[lottozahlen] == 1);

lottopott[lottozahlen] = 1;

}

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if(lottozahlen < lottozahlen[j]){

H = lottozahlen;
lottozahlen = lottozahlen[j];
lottozahlen[j] = H;

}

}
}



return lottozahlen;

}

int treffer (int[] eingabe, int[] lottozahlen){
// Treffer werden gezaehlt

int trefferzaehler = 0;

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if (eingabe == lottozahlen[j])
trefferzaehler++;

}
}

return trefferzaehler;

}

void ausgabe (int[] lottozahlen, int[] eingabe, int treffer){
// Ausgabe von lottozahlen, eingabe & treffer


JOptionPane.showMessageDialog(null, "Lottozahlen: "+lottozahlen);
JOptionPane.showMessageDialog(null, "Tipps: "+eingabe);
JOptionPane.showMessageDialog(null, ""+trefferzaehler);

}

public static void main(String[] args) {
// Methodenaufruf von eingabe, lottozahlen & treffer

Lottozahlen meinLotto = new Lottozahlen();
int[] eingabe = meinLotto.eingabe();
int[] lottozahlen = meinLotto.lottozahlen();
int treffer = meinLotto.treffer(eingabe, lottozahlen);
meinLotto.ausgabe(lottozahlen, eingabe, treffer);

}
}[/highlight]

kA ob alles genau passt - hab leider vorlesung :p und musste schnell gehen
 
Zuletzt bearbeitet:

JeromeM90

Aktives Mitglied
So ich habe mein Programm zum laufen bekommen.
Ich kann jetzt 6 Zahlen eingeben und 6 Zahlen generieren lassen.

Mein Problem ist jetzt nur noch der trefferzaehler.
Ich habe meine Lottozahlen erstmal von 1 -10 gesetzt.

Ich habe mein trefferzaehler eigendlich als Integer.
Aber ich muss mein trefferzaehler doch zum String machen,
da ich meine Methodenaufrufe alle (außer ausgabe) in String habe
und es nicht erlaubt ist dies zu tun:

meinLotto.ausgabe(eingabe, lottozahlen, treffer);
// Dies ist nicht erlaubt (String, String, int)!

Hier mein bisheriger Quellcode:

[highlight=Java]package lottozahlen;

/*
********************************************************************************
* Programmname: Lottozahlen *
* Datum: 7.04.2009 *
* Erstellt von: Jerome *
* *
* Beschreibung: *
* *
* Es sollen 6 Zahlen eingegeben werden und 6 automatisch erzeugt werden. *
* Dieses Programm vergleicht die 6 Zahlen und gibt die Übereinstimmungen aus. *
********************************************************************************
*/

import javax.swing.*;

public class Lottozahlen {

String eingabe (String dialogtext){
// Eingabe der 6 Tipps

final int num_zahl = 6;
final int anz_pott = 10;

int[] tipp = new int [num_zahl];
int[] tipppott = new int [anz_pott+1];

int H;
String sTipp = dialogtext;

for (int i = 0; i <= anz_pott; i++){

tipppott = 0;

}

for (int i = 0; i < num_zahl; i++){

do{

tipp = Integer.parseInt(JOptionPane.showInputDialog("Ihre " +(i+1)+ ". Tippzahl lautet: "));

} while (tipp>49 || (tipppott[tipp]==1) || tipp<1);

tipppott[tipp] = 1;

}

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if(tipp < tipp[j]){

H = tipp;
tipp = tipp[j];
tipp[j] = H;

}

}
}

for (int i = 0; i <= 5; i++){

sTipp = sTipp + tipp + ", ";

}

return sTipp;

}

String lottozahlen (String dialogtext){
// Lottozahlen werden generiert

final int num_zahl = 5;
final int anz_pott = 10;

int[] lottozahlen = new int [num_zahl+1];
int[] lottopott = new int [anz_pott+1];
int H;

String sLotto = dialogtext;

for (int i = 0; i <= anz_pott; i++){

lottopott = 0;

}

for (int i = 0; i <= num_zahl; i++){

do {

lottozahlen = (int)(Math.random()*anz_pott)+1;

} while (lottopott[lottozahlen] == 1);

lottopott[lottozahlen] = 1;

}

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if(lottozahlen < lottozahlen[j]){

H = lottozahlen;
lottozahlen = lottozahlen[j];
lottozahlen[j] = H;

}

}
}

for (int i = 0; i < num_zahl; i++){

sLotto = sLotto + lottozahlen + ", ";

}

return sLotto;

}

String treffer (String eingabe, String lottozahlen){
// Treffer werden gezaehlt

int trefferzaehler = 0;

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if (eingabe == lottozahlen){

trefferzaehler++;

}

}
}

String treffer = Integer.toString(trefferzaehler);
return treffer;

}

void ausgabe (String lottozahlen, String eingabe, String treffer){
// Ausgabe von lottozahlen, eingabe & treffer

JOptionPane.showMessageDialog(null, lottozahlen);
JOptionPane.showMessageDialog(null, eingabe);
JOptionPane.showMessageDialog(null, "Sie haben " +treffer+ " Übereinstimmungen!");

}

public static void main(String[] args) {
// Methodenaufruf von eingabe, lottozahlen & treffer

Lottozahlen meinLotto = new Lottozahlen();
String eingabe = meinLotto.eingabe("Sie haben folgende Zahlen gewählt: ");
String lottozahlen = meinLotto.lottozahlen("Es wurden folgende Zahlen gezogen: ");
String treffer = meinLotto.treffer(eingabe, lottozahlen);
meinLotto.ausgabe(lottozahlen, eingabe, treffer);

}
}[/highlight]

Wenn ich also treffer als String habe zeigt er mir immer an "Sie haben 0 Übereinstimmungen!".

EDIT: Ich müsste theoretisch bei mein String treffer das if-Statement ändern auf:

if (tipp == lottozahlen[j]) <- aber wie gebe ich die Parameter hierher ?
 
Zuletzt bearbeitet:

Matt

Aktives Mitglied
Also ich erklär des nochmal xD du gibst Strings zurück wo popo drin steht. Und popo besteht aus irgendwas braunen und einpaar zahlen....und dieses braune und zahlen versuchst du in strings zuverwandeln und zu vergleichen ^^ das erzeugt noch viel mehr braunes ^^

ich hab ne verbesserung hinzugefügt xD und diese sollte doch passen? oder etwa nicht? Dabei werden nicht mehr strings hin und her geschoben xD sondern nur integer-arrays und integer xD
 

JeromeM90

Aktives Mitglied
Du hast aber gesagt, dass ich den String nicht zum Integerwert machen soll.
Kannst Du mir nicht für diesen Teil einen funktionsfähigen Code vorstellen bitte ?

Dieser String treffer muss geändert werden (aber wie ?):

[highlight=Java]String treffer (String eingabe, String lottozahlen){
// Treffer werden gezaehlt

int trefferzaehler = 0;

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if (eingabe == lottozahlen){

trefferzaehler++;

}

}
}

String treffer = Integer.toString(trefferzaehler);
return treffer;

}[/highlight]

Es geht um die Zeile 9, da er dies nicht vergleichen kann und immer 0 sagt ;).
 

max40

Bekanntes Mitglied
String vergleicht man mit equals. Also if (eingabe.equals(lottozahlen)){ ...

Aber du solltest dir mal durchlesen was Matt-Softwareschmiede schreibt! Dann würdest du auch nicht versuchen 2 Strings miteinander zu vergleichen!
 
Zuletzt bearbeitet:

JeromeM90

Aktives Mitglied
Das ist eine gleiche Variante wie if (eingabe == lottozahlen) ;) und es kommt immmernoch 0 dabei raus.
 

max40

Bekanntes Mitglied
Das ist eine gleiche Variante wie if (eingabe == lottozahlen) ;) und es kommt immmernoch 0 dabei raus.

Definitiv ist es nicht die gleiche Variante! Denn == prüft auf gleiche Referenz nicht auf gleichen Inhalt!

Aber was ich im letzten Beitrag noch geändert habe:
Du solltest das lesen was Matt-Softwareschmiede so schreibt, evtl. hilft das weiter und würdest nicht auf String vergleichen!

Den deine for-schleife gibt dir immer 0 oder 36 zurück und nix anderes. Bestimmt nicht das was du willst!
 

Civilazi

Bekanntes Mitglied
Nein, das ist nicht das Gleiche. == vergleicht Referenzen, equals() kannst du überschreiben, dann tut es was immer du willst. Im Allgemeinen prüft es auf gleichen Inhalt.

€: Zu lahm... na ja :)
 

JeromeM90

Aktives Mitglied
Ah supi.
Denn habe ich was Neues gelernt.
Ich komme mit der Aussage von Matt... nicht klar.

Vielleicht kann er mir ja bitte ein Beispiel meines Programmes geben ;).
 

max40

Bekanntes Mitglied
[HIGHLIGHT="Java"] int treffer (int[] eingabe, int[] lottozahlen){
// Treffer werden gezaehlt

int trefferzaehler = 0;

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if (eingabe == lottozahlen[j])
trefferzaehler++;

}
}

return trefferzaehler;

}[/HIGHLIGHT]


Statt irgendwelche String zu vergleichen, packt er die Zahlen in ein int[] und vergleicht deren Inhalte!
 

Matt

Aktives Mitglied
^^ jaja nehmen wir mal deine funktion

[highlight="Java"]String treffer (String eingabe, String lottozahlen){
// Treffer werden gezaehlt

int trefferzaehler = 0;

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if (eingabe == lottozahlen){

trefferzaehler++;

}

}
}

String treffer = Integer.toString(trefferzaehler);
return treffer;

}[/highlight]

So und nun nehmen wir meinen verbesserungsvorschlag :D welcher zwischen Zeile 117 und 133 steht ;)
[highlight="Java"]
int treffer (int[] eingabe, int[] lottozahlen){
// Treffer werden gezaehlt

int trefferzaehler = 0;

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if (eingabe == lottozahlen[j])
trefferzaehler++;

}
}

return trefferzaehler;

}[/highlight]

Nun bitte den unterschied suchen :D
 
Zuletzt bearbeitet:

Schandro

Top Contributor
Braces are used around all statements, even single statements, when they are part of a control structure, such as a if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces.
Aus der Java Coding Convention. Es kann aber natürlich jeder machen wie er will.
 

JeromeM90

Aktives Mitglied
Ok.
Das werde ich später dann ausprobieren.
Ich muss erstmal weg und vielen Dank für dein Beispiel.
Ich habe es kappiert.
 

JeromeM90

Aktives Mitglied
So ich habe das geändert.
Nun bleibt noch der Methodenaufruf offen.

Fehler: String treffer = meinLotto.treffer(eingabe, lottozahlen);
// The method treffer(int[], int[]) in the type Lottozahlen is not applicable for the arguments (String, String)

Matt-Softwareschmiede, könntest Du bitte mein komplettes Programm überarbeiten,
sodass der trefferzaehler funktioniert und das Programm geht ?

VIELEN DANK SCHONMAL!
 
M

MiDniGG

Gast
So ich habe das geändert.
Nun bleibt noch der Methodenaufruf offen.

Fehler: String treffer = meinLotto.treffer(eingabe, lottozahlen);
// The method treffer(int[], int[]) in the type Lottozahlen is not applicable for the arguments (String, String)

Matt-Softwareschmiede, könntest Du bitte mein komplettes Programm überarbeiten,
sodass der trefferzaehler funktioniert und das Programm geht ?

VIELEN DANK SCHONMAL!

Der Fehler sagt Dir, dass Du versuchst ein int-Array zu übergeben, obwohl Strings erwartet werden.
Ich hab mir Dein Prog jetzt nicht angeschaut. Aber entweder musst Du eben Strings anstatt der int-Arrays an die Methode treffer übergeben oder die Methode treffer selbst umschreiben, sodass sie int-Arrays erwartet
[HIGHLIGHT="Java"]public void treffer(int[] arr1, int[] arr2) {...}[/HIGHLIGHT]
Jedoch wirst Du hierbei noch ziemlich viel in der Methode treffer selbst umschreiben müssen... ;)
 

Matt

Aktives Mitglied
und nochmal :)

Dein Code:
[highlight="Java"] public static void main(String[] args) {
// Methodenaufruf von eingabe, lottozahlen & treffer

Lottozahlen meinLotto = new Lottozahlen();
String eingabe = meinLotto.eingabe("Sie haben folgende Zahlen gewählt: ");
String lottozahlen = meinLotto.lottozahlen("Es wurden folgende Zahlen gezogen: ");
String treffer = meinLotto.treffer(eingabe, lottozahlen);
meinLotto.ausgabe(lottozahlen, eingabe, treffer);

}
[/highlight]
Mein Code:

[highlight="Java"]public static void main(String[] args) {
// Methodenaufruf von eingabe, lottozahlen & treffer

Lottozahlen meinLotto = new Lottozahlen();
int[] eingabe = meinLotto.eingabe();
int[] lottozahlen = meinLotto.lottozahlen();
int treffer = meinLotto.treffer(eingabe, lottozahlen);
meinLotto.ausgabe(lottozahlen, eingabe, treffer);

}
[/highlight]


Das geile an der sache ist Oo das steht alles auf Seite 1 von diesem Topic/Thread....es sind alle funktionen deiner klasse nur umgeschrieben so das es passt ^^ ein Copy und Paste hätte gereicht und du wärst schon vor stunden fertig gewesen....


Matt
 
Zuletzt bearbeitet:

JeromeM90

Aktives Mitglied
Oh Danke Dir und sorry das ich mich so anstelle, aber ich bin noch Anfänger und verstehe noch nicht alles.
Aber dafür wird ja hier geholfen und mir hilft das weiter.

EDIT: LoL jetzt gibt er mir nur Müll aus. Aber Übereinstimmungen gehen.

[highlight=Java]package lottozahlen;

/*
********************************************************************************
* Programmname: Lottozahlen *
* Datum: 7.04.2009 *
* Erstellt von: Jerome *
* *
* Beschreibung: *
* *
* Es sollen 6 Zahlen eingegeben werden und 6 automatisch erzeugt werden. *
* Dieses Programm vergleicht die 6 Zahlen und gibt die Übereinstimmungen aus. *
********************************************************************************
*/

import javax.swing.*;

public class Lottozahlen {

int[] tipp (){
// Eingabe der 6 Tipps

final int num_zahl = 5;
final int anz_pott = 10;

int[] tipp = new int [num_zahl+1];
int[] tipppott = new int [anz_pott+1];

int H;

for (int i = 0; i <= anz_pott; i++){

tipppott = 0;

}

for (int i = 0; i < num_zahl; i++){

do{

tipp = Integer.parseInt(JOptionPane.showInputDialog("Ihre " +(i+1)+ ". Tippzahl lautet: "));

} while (tipp>49 || (tipppott[tipp]==1) || tipp<1);

tipppott[tipp] = 1;

}

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if(tipp < tipp[j]){

H = tipp;
tipp = tipp[j];
tipp[j] = H;

}

}
}

return tipp;

}

int[] lottozahlen (){
// Lottozahlen werden generiert

final int num_zahl = 5;
final int anz_pott = 10;

int[] lottozahlen = new int [num_zahl+1];
int[] lottopott = new int [anz_pott+1];

int H;

for (int i = 0; i <= anz_pott; i++){

lottopott = 0;

}

for (int i = 0; i <= num_zahl; i++){

do {

lottozahlen = (int)(Math.random()*anz_pott)+1;

} while (lottopott[lottozahlen] == 1);

lottopott[lottozahlen] = 1;

}

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if(lottozahlen < lottozahlen[j]){

H = lottozahlen;
lottozahlen = lottozahlen[j];
lottozahlen[j] = H;

}

}
}

return lottozahlen;

}

int treffer (int[] tipp, int[] lottozahlen){
// Treffer werden gezaehlt

int trefferzaehler = 0;

for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 5; j++){

if (tipp == lottozahlen[j]){

trefferzaehler++;

}

}
}

return trefferzaehler;

}

void ausgabe (int[] tipp, int[] lottozahlen, int treffer){
// Ausgabe von lottozahlen, eingabe & treffer

JOptionPane.showMessageDialog(null, "Ihre Tippzahlen lauten: " +tipp);
JOptionPane.showMessageDialog(null, "Es wurden folgende Lottozahlen ermittelt: " +lottozahlen);
JOptionPane.showMessageDialog(null, "Sie haben " +treffer+ " Übereinstimmungen!");

}

public static void main(String[] args) {
// Methodenaufruf von eingabe, lottozahlen & treffer

Lottozahlen meinLotto = new Lottozahlen();
int[] tipp = meinLotto.tipp();
int[] lottozahlen = meinLotto.lottozahlen();
int treffer = meinLotto.treffer(tipp, lottozahlen);
meinLotto.ausgabe(lottozahlen, tipp, treffer);

}
}[/highlight]
 
Zuletzt bearbeitet:
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
M Problem mit meinem Programm Java Basics - Anfänger-Themen 6
megachucky Problem bei meinem Videothek-Programm Java Basics - Anfänger-Themen 5
N Problem bei meinem Code Java Basics - Anfänger-Themen 10
F Methoden Kleines Problem mit meinem Glücksrad... Java Basics - Anfänger-Themen 9
D Problem mit meinem ersten JOptionPane - Fatal exception occurred. Program will exit. Java Basics - Anfänger-Themen 6
S Ich habe ein kleines Problem mit meinem Übungsprogramm Java Basics - Anfänger-Themen 7
G Was is falsch mit meinem program? (stringvergleich-problem) Java Basics - Anfänger-Themen 8
K Verständnis Problem bei Server/Client Java Basics - Anfänger-Themen 2
I WildFily - unterschiedliche Libs im Projekt verursachen Problem Java Basics - Anfänger-Themen 11
imocode Vererbung Problem mit Vererbung Java Basics - Anfänger-Themen 2
L Taschenrechner Problem Java Basics - Anfänger-Themen 4
I Applikationsserver (WildFly) - Zugriff auf Ressourcen.. Problem mit Pfade Java Basics - Anfänger-Themen 10
A ScheduledExecutorService problem Java Basics - Anfänger-Themen 7
marcelnedza Problem mit Weltzuweisung, JavaKarol Java Basics - Anfänger-Themen 13
XWing Methoden rückgabe Problem? Java Basics - Anfänger-Themen 6
M Erste Schritte Collatz Problem max int Java Basics - Anfänger-Themen 3
M Problem bei verschachtelter for-Schleife bei zweidimensionalen Arrays Java Basics - Anfänger-Themen 3
C GLOOP Problem beim Erstellen der Kamera Java Basics - Anfänger-Themen 9
nelsonmandela Problem bei Ausgabe einer Switch - Case Funktion Java Basics - Anfänger-Themen 5
frager2345 Problem mit Methode Java Basics - Anfänger-Themen 4
L Problem bei Rechnung mit Math.pow Java Basics - Anfänger-Themen 13
A Thread-Schreibe-Lese-Problem Java Basics - Anfänger-Themen 4
SUPERTJB return Problem Java Basics - Anfänger-Themen 3
sserio BigInteger Problem Java Basics - Anfänger-Themen 4
JordenJost Taschenrechner problem Java Basics - Anfänger-Themen 5
K Problem mit "Random" Java Basics - Anfänger-Themen 5
S Datei anlegen Problem! Groß- und Kleinschreibung wird nicht unterschieden Java Basics - Anfänger-Themen 4
sserio Problem beim Anzeigen Java Basics - Anfänger-Themen 5
xanxk Problem For-Schleife mit Charakter Java Basics - Anfänger-Themen 2
L Unbekanntes Problem mit 2d Array Java Basics - Anfänger-Themen 6
sserio Liste erstellt und ein Problem mit dem Index Java Basics - Anfänger-Themen 8
sserio Schwimmen als Spiel. Problem mit to String/ generate a card Java Basics - Anfänger-Themen 4
J Schleife Problem Java Basics - Anfänger-Themen 2
D Problem mit der Erkennung von \n Java Basics - Anfänger-Themen 2
milan123 das ist meine aufgabe ich hab das problem das bei mir Wenn ich die Richtung der Linien verändern will und drei davon sind richtig, verändere ich die 4 Java Basics - Anfänger-Themen 3
M Verständins Problem bei Aufgabe Java Basics - Anfänger-Themen 4
HeiTim Problem mit der Kommasetzung an der richtigen stelle Java Basics - Anfänger-Themen 59
Temsky34 Problem mit dem Code Java Basics - Anfänger-Themen 17
P Problem mit Calendar.getDisplayName() Java Basics - Anfänger-Themen 8
C Problem mit mehreren Methoden + Scanner Java Basics - Anfänger-Themen 5
P Datei einlesen, nach Begriff filtern und in Datei ausgeben. Problem Standardausgabe über Konsole Java Basics - Anfänger-Themen 19
M Problem mit Klassenverständnis und Button Java Basics - Anfänger-Themen 8
EchtKeineAhnungManchmal hallo habe ein Problem mit einer Datei -> (Zugriff verweigert) Java Basics - Anfänger-Themen 4
H Problem mit Verzweigungen Java Basics - Anfänger-Themen 6
H Problem mit Rückgabewert Java Basics - Anfänger-Themen 7
josfe1234 JAVA FX problem Java Basics - Anfänger-Themen 3
A Code Problem Java Basics - Anfänger-Themen 6
Henri Problem von Typen Java Basics - Anfänger-Themen 7
J Problem mit "ArrayIndexOutOfBoundsException" Java Basics - Anfänger-Themen 11
K jackson Mapping - Problem mit Zeitzonen Java Basics - Anfänger-Themen 10
B Threads Problem mit mehreren Threads Java Basics - Anfänger-Themen 38
I Output BigDecimal anstatt double / Problem beim Rechnen Java Basics - Anfänger-Themen 16
D Schleifen Problem Java Basics - Anfänger-Themen 2
H So viele Fehlermeldungen, dass ich nicht weiß wo das Problem ist. Java Basics - Anfänger-Themen 6
J JAVA-Problem blockiert MEDIATHEKVIEW Java Basics - Anfänger-Themen 13
T Problem mit Lehrzeichen und String bei einfacher Chiffre Java Basics - Anfänger-Themen 8
J extends Problem Java Basics - Anfänger-Themen 2
C Polymorphie-Problem Java Basics - Anfänger-Themen 3
Kalibru Problem bei Ausgabe von Objekt Java Basics - Anfänger-Themen 1
I Format Problem mit Wert - bekomme 0,10 anstatt 10,00 Java Basics - Anfänger-Themen 6
J Problem mit einer Methode die gewissen Inhalt einer Array löschen soll Java Basics - Anfänger-Themen 9
J Problem mit einer Methode, die beliebig viele Objekte in Array speichern soll Java Basics - Anfänger-Themen 6
J Allgemeines Problem mit Klassen Java Basics - Anfänger-Themen 5
U Problem mit dem initialisieren meines Strings in einer Schleife Java Basics - Anfänger-Themen 5
amgadalghabra algorithmisches Problem Java Basics - Anfänger-Themen 19
J Traveling Salesman Problem [Arrays] Java Basics - Anfänger-Themen 9
R ArrayList Problem Java Basics - Anfänger-Themen 6
InfinityDE Problem mit Datenübergabe an Konstruktor Java Basics - Anfänger-Themen 7
C RegEx Problem Java Basics - Anfänger-Themen 4
J Anfänger TicTacToe, Problem bei Gewinnoption, sowohl Unentschieden Java Basics - Anfänger-Themen 8
E Taschenrechner GUI Problem mit Fehlerhandling Java Basics - Anfänger-Themen 6
M Input/Output Fallunterscheidung Problem Java Basics - Anfänger-Themen 17
P Problem beim Überschreiben einer vererbten Methode Java Basics - Anfänger-Themen 4
M Problem bei Ausgabe Java Basics - Anfänger-Themen 7
Splayfer Java Array Problem... Java Basics - Anfänger-Themen 2
G Problem bei der Ausgabe einer Main Claase Java Basics - Anfänger-Themen 7
F Problem mit KeyListener in kombination mit dem ActionListener Java Basics - Anfänger-Themen 4
G Subset sum problem mit Backtracking Java Basics - Anfänger-Themen 18
N Problem mit Scanner Java Basics - Anfänger-Themen 2
J Klassen Problem Java Basics - Anfänger-Themen 8
A Out.format problem. Java Basics - Anfänger-Themen 3
J Problem bei der Programmierung eines Tannenbaums Java Basics - Anfänger-Themen 9
A Array problem Java Basics - Anfänger-Themen 16
2 Taschenrechner mit GUI Problem bei der Berechnung Java Basics - Anfänger-Themen 8
W Remote Method Invocation RMI - Problem Java Basics - Anfänger-Themen 0
I Ich habe ein Problem Java Basics - Anfänger-Themen 3
A Problem bei returnen eines Wertes Java Basics - Anfänger-Themen 6
M Regex Erstellung Problem Java Basics - Anfänger-Themen 2
D Input/Output Problem bei der Benutzereingabe eines Befehls Java Basics - Anfänger-Themen 14
M (Sehr großes Problem) Listen als static in anderen Klassen verwendet Java Basics - Anfänger-Themen 12
F Habe ein problem mit dem ActionListener Java Basics - Anfänger-Themen 3
C Regex-Problem Java Basics - Anfänger-Themen 4
J Problem beim vergleich von zwei Integer Java Basics - Anfänger-Themen 3
M Problem in der Modellierung Java Basics - Anfänger-Themen 20
W Wo ist das URL-Problem ? Java Basics - Anfänger-Themen 1
S Generics-Problem: Class, Class<?>, Class<Object> Java Basics - Anfänger-Themen 4
D FileWriter / FileReader Problem Java Basics - Anfänger-Themen 10
G Problem beim Speichern von Objekten in einer Datei Java Basics - Anfänger-Themen 7
S Compiler-Fehler Exception in thread "main" java.lang.Error: Unresolved compilation problem: Java Basics - Anfänger-Themen 6
J Problem mit Array: 2 Klassen Java Basics - Anfänger-Themen 2

Ähnliche Java Themen

Neue Themen


Oben