initial commit
This commit is contained in:
commit
445d9ea235
|
|
@ -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!
|
||||||
|
|
@ -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<Integer> 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<Integer> 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<Integer> 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue