From 445d9ea2351b4251cbe2719be876af3daf0e8b02 Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Wed, 8 Apr 2020 17:38:27 +0200 Subject: [PATCH] initial commit --- README.md | 5 +++ src/gfn/marc/Lottozahlen.java | 74 +++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 README.md create mode 100644 src/gfn/marc/Lottozahlen.java diff --git a/README.md b/README.md new file mode 100644 index 0000000..442056a --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Aufgabe ArrayList Lottozahlen: + +a) Lassen Sie die zufälligen Lottozahlen 6 aus 49 ziehen, setzen Sie diese in eine ArrayList lottozahlen. + +b) Geben Sie Ihre getippten eigenen 6 Zahlen ein und ermitteln Sie die Anzahl der Richtigen! \ No newline at end of file diff --git a/src/gfn/marc/Lottozahlen.java b/src/gfn/marc/Lottozahlen.java new file mode 100644 index 0000000..c78483c --- /dev/null +++ b/src/gfn/marc/Lottozahlen.java @@ -0,0 +1,74 @@ +package gfn.marc; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Scanner; + +public class Lottozahlen { + + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + boolean eingabeKorrekt = false; + + // Lottoschein ausfüllen + ArrayList lottoschein = new ArrayList<>(6); + while (!eingabeKorrekt) { + System.out.println("Bitte geben Sie sechs, mit Kommata getrennte, Lottozahlen (1 - 49) ein. " + + "(Beispiel: \"6,12,29,34,40,46\")"); + String eingabe = scanner.next(); + if (eingabe.matches("([1-4][0-9][,]|[0-9][,]){5}[1-4][0-9]|[0-9]")) { + + String[] zahlen = eingabe.split("[,]"); + + for (String zahl : zahlen) { + lottoschein.add(Integer.parseInt(zahl)); + } + + eingabeKorrekt = true; + } else { + System.err.println("Ungültige Zahlen!"); + } + } + + // Lottozahlen ziehen + ArrayList lottozahlen = new ArrayList<>(6); + for (int i = 0; i <= 6; i++) { + lottozahlen.add((int) (Math.random() * 49) + 1); + } + + // Lottozahlen ausgeben + System.out.println(); + System.out.print("Lottozahlen: "); + for (int i = 0; i < lottozahlen.size(); i++) { + if (i < lottozahlen.size() - 1) { + System.out.print(lottozahlen.get(i) + ", "); + } else { + System.out.print(lottozahlen.get(i)); + } + } + System.out.println("\n"); + + // Lottoschein auswerten + ArrayList richtige = new ArrayList<>(); + for (int zahl : lottoschein ) { + if (lottozahlen.contains(zahl)) { + richtige.add(zahl); + } + } + + + Collections.sort(richtige); + System.out.println(richtige.isEmpty() ? "Sie haben leider keine richtige Zahl!" : + "Sie haben " + richtige.size() + " Richtige: "); + if (!richtige.isEmpty()) { + for (int i = 0; i < richtige.size(); i++) { + if (i < richtige.size() - 1) { + System.out.print(richtige.get(i) + ", "); + } else { + System.out.print(richtige.get(i)); + } + } + } + } +}