From c41c0d5c4cc9e2933a298923b20d483c23a6b0f8 Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Thu, 9 Apr 2020 08:55:52 +0200 Subject: [PATCH] =?UTF-8?q?Pr=C3=BCfung=20auf=20doppelte=20Zahlen=20hinzug?= =?UTF-8?q?ef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gfn/marc/Lottozahlen.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/gfn/marc/Lottozahlen.java b/src/gfn/marc/Lottozahlen.java index c78483c..9771a08 100644 --- a/src/gfn/marc/Lottozahlen.java +++ b/src/gfn/marc/Lottozahlen.java @@ -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 lottoschein = new ArrayList<>(6); + HashSet 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)); } - eingabeKorrekt = true; + // 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 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: ");