Prüfung auf doppelte Zahlen hinzugefügt

This commit is contained in:
Marc Koch 2020-04-09 08:55:52 +02:00
parent 445d9ea235
commit c41c0d5c4c
1 changed files with 11 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package gfn.marc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
public class Lottozahlen {
@ -12,7 +13,7 @@ public class Lottozahlen {
boolean eingabeKorrekt = false;
// Lottoschein ausfüllen
ArrayList<Integer> lottoschein = new ArrayList<>(6);
HashSet<Integer> lottoschein = new HashSet<>(6);
while (!eingabeKorrekt) {
System.out.println("Bitte geben Sie sechs, mit Kommata getrennte, Lottozahlen (1 - 49) ein. " +
"(Beispiel: \"6,12,29,34,40,46\")");
@ -25,7 +26,13 @@ public class Lottozahlen {
lottoschein.add(Integer.parseInt(zahl));
}
// Prüfung: Doppelte zahlen?
if (lottoschein.size() == 6) {
eingabeKorrekt = true;
} else {
System.err.println("Doppelte Zahlen!");
}
} else {
System.err.println("Ungültige Zahlen!");
}
@ -51,13 +58,13 @@ public class Lottozahlen {
// Lottoschein auswerten
ArrayList<Integer> richtige = new ArrayList<>();
for (int zahl : lottoschein ) {
for (int zahl : lottoschein) {
if (lottozahlen.contains(zahl)) {
richtige.add(zahl);
}
}
// Ergebnis ausgeben
Collections.sort(richtige);
System.out.println(richtige.isEmpty() ? "Sie haben leider keine richtige Zahl!" :
"Sie haben " + richtige.size() + " Richtige: ");