Hallo! Für die Klausurvorbereitung mache ich grade ein paar Aufgaben und eine lautet wie folgt: "Implementieren Sie die Ziehung der Lottozahlen (6 aus 49) mit Hilfe einer Liste! Die Liste soll aus den Zahlen von 1 bis 49 bestehen. Wenn eine Zahl gezogen wurde, soll sie aus der Liste ausgekettet werden." - dabei ist die Verwendung der Collection-Klassen untersagt.
Nach ewig langem Hin und Her habe ich jetzt auch eine Lösung, die auf den ersten Blick gut aussah - sich auf den zweiten allerdings als falsch herausstellte - irgendwie werden manchmal 5 oder auch 7 Zahlen "gezogen" und das sind dann auch noch andere als meine Math.random() Methode erstellt hat. Hier ist mein Code: (Pastebin Link: https://pastebin.com/cuq3QYaF)
Nach ewig langem Hin und Her habe ich jetzt auch eine Lösung, die auf den ersten Blick gut aussah - sich auf den zweiten allerdings als falsch herausstellte - irgendwie werden manchmal 5 oder auch 7 Zahlen "gezogen" und das sind dann auch noch andere als meine Math.random() Methode erstellt hat. Hier ist mein Code: (Pastebin Link: https://pastebin.com/cuq3QYaF)
Java:
public class Aufg1LottoZahl {
int zahl;
Aufg1LottoZahl next;
Aufg1LottoZahl prev;
public Aufg1LottoZahl(int zahl) {
this.zahl = zahl;
}
public Aufg1LottoZahl() {
this.zahl = -1;
}
public int getZahl() {
return zahl;
}
public void setNext(Aufg1LottoZahl next) {
this.next = next;
}
public Aufg1LottoZahl getNext() {
return next;
}
public void setPrev(Aufg1LottoZahl next) {
this.next = next;
}
public Aufg1LottoZahl getPrev() {
return next;
}
}
Java:
public class Aufg1LottoMain {
public static void main(String[] args) {
Aufg1LottoZahl[] lottozahlen = new Aufg1LottoZahl[49];
for (int i = 0; i < lottozahlen.length; i++) {
lottozahlen[I] = new Aufg1LottoZahl(i + 1);
if (i > 0 && i < 48) {
lottozahlen[I].setNext(lottozahlen[i + 1]);
lottozahlen[I].setPrev(lottozahlen[i - 1]);
}
else if (i == 48) {
lottozahlen[I].setNext(lottozahlen[0]);
lottozahlen[I].setPrev(lottozahlen[i - 1]);
}
else if (i == 0) {
lottozahlen[I].setPrev(lottozahlen[48]);
lottozahlen[I].setNext(lottozahlen[i + 1]);
}
}
ziehung(lottozahlen);
}
public static void ziehung(Aufg1LottoZahl[] lottozahlen) {
int randomZahl;
for (int i = 0; i < 6; i++) {
randomZahl = (int) (Math.random() * 49);
while (lottozahlen[randomZahl].getNext() == null && lottozahlen[randomZahl].getPrev() == null) {
randomZahl = (int) (Math.random() * 49);
}
System.out.print(randomZahl + " "); //Ausgabe der 6 erstellten Random Zahlen zum Abgleich mit den eigentlichen 6 Zahlen
(lottozahlen[randomZahl].getPrev()).setNext(lottozahlen[randomZahl].getNext());
(lottozahlen[randomZahl].getNext()).setPrev(lottozahlen[randomZahl].getPrev());
lottozahlen[randomZahl].setNext(null);
lottozahlen[randomZahl].setPrev(null);
}
System.out.println();
for (int i = 0; i < 49; i++) {
if (lottozahlen[I].getNext() == null && lottozahlen[I].getPrev() == null) {
System.out.print(lottozahlen[I].getZahl() + " ");
}
}
}
}
Zuletzt bearbeitet: