Die Prüfung für Benutzereingaben werden nur ganze zahlen in Java

Mache ich-Lotto-Spiel für meine Aufgabe (Benutzer Eingänge 6 Anzahl Generiere ich 8 einzigartige Gewinnzahlen und die 2 letzten zahlen sind Ergänzende). Ich brauche Hilfe mit dem Benutzer-input überprüfen, ob die Eingabe zahlen von 1 bis 45 und die Eingabe muß vom Datentyp int sein, wenn die Eingabe keine ganze Zahl ist, es wirft einen Fehler.

Diese Programmierung Weg ist die Vorgangsweise, wie kann ich es ändern, in der Objekt-orientierten Art und Weise? Ich weiß, dass ich muss machen Methoden in einer anderen java-Datei und dann verlinken Sie zurück zu diesem main. Können Sie mir empfehlen, wie es zu tun?

Ich habe versucht zu versuchen und zu fangen, if und else (Eingabe überprüfen), aber ich weiß nicht, wie Benutzereingaben überprüft wird, wenn es im array. Danke für die Hilfe.

Hier ist mein code:

class Lottery {

public static void main ( String[] args ) {

    System.out.println("\nWelcome to the Lottery game.");
    System.out.println("You can enter numbers from 1 to 45.");

    //User input into an array
    int[] input = new int[6];

    Scanner scanner = new Scanner(System.in);

    System.out.println("\nPlease enter your 6 lucky numbers: ");
    for(int j = 0; j < 6; j++) {

        input[j]=scanner.nextInt();
    }

    int check = scanner.nextInt();
    if(check < 0 && check > 45) {
        System.out.println("\nERROR: Please enter only numbers from 1 to 45!");
    }

    //Printing out unique winning numbers from random generator
    System.out.println("\nWinning numbers: ");
    MultiRandomGenerator mrg = new MultiRandomGenerator();
    int[] set;

    set = mrg.getSet();
    for (int i = 0; i < set.length; i++) {

        System.out.print(set[i] + " ");
    }

    //Loops for counting how many numbers user has guessed right
    int count = 0; //for 6 numbers
    int scount = 0; //for 2 last supplementary numbers

    for(int i = 0; i < input.length; i++) {

        for(int k = 0; k < set.length; k++) {

            if (k < 6) {

                if (set[k] == input[i]) {

                    count++;
                } else {
                    if (set[k] == input[i]) {

                    scount++;
                    }

                }

            }

        }
    }
    System.out.print("\n\nYou guessed right " + count + " winning numbers.");
    System.out.print("\nYou guessed right " + scount + " suplementary numbers.");

    //If statments for printing out winning prizes
    if (count == 6) {

        System.out.println("\nYou have won 1st price!");
    } if (count == 5 && scount == 1) {

        System.out.println("\nYou have won 2st price!");
    } if (count == 5) {

        System.out.println("\nYou have won 3st price!");
    } if (count == 4) {

        System.out.println("\nYou have won 4st price!");
    } if (count == 3 && scount == 1) {

        System.out.println("\nYou have won 5st price!");
    } if (count == 1 && scount == 2) {

        System.out.println("\nYou have won 6st price!");
    } else {

        System.out.println("\nSorry, you didn't won anything.");
    }
}
}

InformationsquelleAutor user2272351 | 2013-04-11

Schreibe einen Kommentar