G
Guest
Gast
Verstehe nicht warum meine Ausgabe so spinnt??
Code:
import java.util.Random;
import java.io.*;
public class Lotto{
// Zähler für zufall
static int count = 1;
// zähler für eingabe
static int zähler = 1;
// zähler für richtige
static int richtige = 0;
// Array für Eingabe der lotto Zahlen
static int[] zahl = new int[7];
// Array für Zufallszahl
static int[] x = new int[7];
// Neues object der Klasse Random
static Random rand = new Random();
// String für Eingaben
static String input;
public static void main(String[] agrs) throws IOException {
// starten der Methode zufall() zu Errechnung der Zufallszahlen
zufall();
}
public static void zufall() throws IOException {
// Übergabe an methode eingabe()
eingabe();
count = 1;
// Berechnung und Ausgabe der lotto Zahlen
System.out.println("Es wurd gezogen : ");
while (count < 7) {
x[count] = Math.abs(rand.nextInt()) % 50;
prüfzufall();
// Prüfen ob Zufallszahl = 0
while (x[count] == 0) {
x[count] = Math.abs(rand.nextInt()) % 50;
prüfzufall();
}//Ende while (x[count] == 0)
// Ausgabe der lotto Ziehung
System.out.print(x[count] + " ");
}
// Zähler wieder zurück setzen
count = 1;
zähler = 1;
// Zufallszahl mit zahl[i] prüfen
while (zähler < 7) {
if (x[count] == zahl[zähler]) {
richtige++;
}
count++;
// zahl[i+1] zur Prüfung der nächsten eingabe Zahl
if (count == 7) {
count = 0;
zähler++;
}
}
if (richtige > 0){
System.out.println("\n\n\n" + "Sie haben " + richtige
+ " richtig!");}
}
public static void eingabe() throws IOException {
// Erzeugen eines BufferedReader für Eingabe
BufferedReader sdin = new BufferedReader(new InputStreamReader(
System.in));
// Einlesen der Zahlen
while (zähler < 7) {
System.out.println("Bitte ihren " + zähler + " Tipp ein:");
input = sdin.readLine();
// Überprüfung der richtigen Eingabe
try {
zahl[zähler] = Integer.parseInt(input);
// Prüfen auf den Wertebereich
if (zahl[zähler] > 49 || zahl[zähler] == 0) {
System.out.println("\nDer " + zähler +" Tipp ist falsch nur 1 - 49!!");
zähler--;
}//Ende if
// Prüfung auf doppelte Zahlen
while (count < zähler) {
count++;
if (zahl[count - 1] == zahl[zähler]) {
System.out.println("\n Diesen Tipp haben Sie schon!");
zähler--;
}//Ende if
}//Ende while
count = 1;
}
catch (NumberFormatException e) {
System.out.println("Nur Ganzzahlen eingeben" );
// Bei Falscheingabe, wieder das einlesen starten
zähler--;
}
// Zähler erhöhen um 1
zähler++;
}//Ende while (zähler < 7)
System.out.println("\n" + "Sie tippten: ");
for (zähler = 1; zähler < 7; zähler++) {
System.out.print(zahl[zähler] + " ");
}//Ende for (zähler = 1; zähler < 7; zähler++)
System.out.println("\n");
}
public static void prüfzufall() {
// doppelte Zufallszahlen Prüfen
for (int i = 0; i < count;) {
i++;
if (x[count] == x[i - 1]) {
x[count] = Math.abs(rand.nextInt()) % 50;
}
}
}}