简体   繁体   中英

Try/Catch Loop with return value

I'm new to programming, so I just need some help. I kinda figured out some things on my own, but now now I am lost I would say. Keep in mind that I'm not a native speaker, so sorry for my mistakes :) I'm learning Java in my University, but we have to do exercises on our own. The program is about typing in an interval like 1 and 20, the program squares it and I need to find the right answer to it. I have 3 tries to get the correct answer. After the third try I get to ask if I want to try another Number or end the program

The actual problem that I got is, the "try and catch loop", which needs to test that I enter a positive Number.

public static int prüfung(int ZahlPrüfen) {
    while (true) {
        try {

            ZahlPrüfen = IntervallScanner.nextInt();
            if (IntervallScanner.hasNextInt() && ZahlPrüfen > 0) {
                return ZahlPrüfen;

            } else if (ZahlPrüfen <= 0) {
                System.out.println("Bitte Geben Sie eine Positive Zahl ein");
                IntervallScanner.next();

            }

        } catch (InputMismatchException e) {
            System.out.println("Bitte Geben Sie eine Zahl und keinen Buchstaben ein");
            IntervallScanner.next();

        }
    }

The Problem kinda is that, if I enter a positive Number (so everything right), I have to type it in twice! Same when I type in a negative Number, I need to type it twice. I get an "error" when i type in a string, so that works kinda fine i guess. it shows like this:

Bitte geben Sie die kleinste positive Zahl des Intervalls ein: -2

-3

Bitte Geben Sie eine Positive Zahl ein

-4

-5

Bitte Geben Sie eine Positive Zahl ein

-6

-7

Bitte Geben Sie eine Positive Zahl ein

2

2

Bitte geben Sie die Größte Zahl des Intervalls ein: 3

Wie lautet die Wurzel aus 4?

And The code takes the first time when i type in the correct numbers like in this case 2 and 2, and uses them to make the random number, and not 2 and 3.

I hope you can understand what I mean this far. It would be real great if someone of you guys can help me, I'm stuck like for 5 hours and cant figure it out. My brain just stops working :D

Here is the complete code:

public static int zufallszahl = 0;
public static Scanner sc = new Scanner(System.in);
public static Scanner eingabe = new Scanner(System.in);
public static Scanner IntervallScanner = new Scanner(System.in);
public static int AnzahlVersuche = 0;
public static int k = 0;
public static int kleineZahl;
public static int großeZahl;
public static int ZahlPrüfen;
public static int ZahlPrüfung;

public static void main(String[] args) {

    System.out.print("Bitte geben Sie die kleinste positive Zahl des Intervalls ein: ");

    kleineZahl = prüfung(ZahlPrüfen);

    System.out.print("Bitte geben Sie die Größte Zahl des Intervalls ein: ");

    großeZahl = prüfung(ZahlPrüfen);

    do {
        ermittleZufallszahl(großeZahl, kleineZahl);

        int quadrad = (int) Math.pow(zufallszahl, 2);
        System.out.println("Wie lautet die Wurzel aus " + quadrad + "?");

        erneuterVersuch();

    } while (k == 1);
}

public static void ermittleZufallszahl(int max, int min) {
    zufallszahl = (int) ((Math.random() * (max - min + 1)) + min);

}

public static int erneuterVersuch() {
    AnzahlVersuche = 0;
    while (AnzahlVersuche <= 2) {
        int input = sc.nextInt();
        if (input == zufallszahl) {
            System.out.println("Das ist richtig,toll!");
            System.out.println("Wollen Sie eine andere Zahl probieren? Wenn Ja geben Sie (j) ein, wenn Nein geben Sie (n) ein.");
            String eingabe1 = eingabe.nextLine();
            switch (eingabe1) {
                case "j":
                case "J":

                    k = 1;

                    return k;

                case "n":
                case "N":
                    System.out.println("Programm wird jetzt beendet");
                    k = 2;
                    System.exit(0);
            }
        } else {
            System.out.println("Das ist falsch, schade!");

            ++AnzahlVersuche;
            if (AnzahlVersuche == 3) {
                System.out.println("Sie haben es mit 3 Versuchen leider nicht geschafft! Die richtige Antwort wäre " + zufallszahl + " gewesen.");
                System.out.println("Wollen Sie eine andere Zahl probieren? Wenn Ja geben Sie (j) ein, wenn Nein geben Sie (n) ein.");

                String eingabe1 = eingabe.nextLine();
                switch (eingabe1) {
                    case "j":
                    case "J":

                        k = 1;

                        break;
                    case "n":
                    case "N":
                        System.out.println("Programm wird jetzt beendet");
                        k = 2;
                        System.exit(0);
                }

            }

        }
    }
    return k;
}

public static int prüfung(int ZahlPrüfen) {

    while (true) {
        try {

            ZahlPrüfen = IntervallScanner.nextInt();
            if (IntervallScanner.hasNextInt() && ZahlPrüfen > 0) {
                return ZahlPrüfen;

            } else if (ZahlPrüfen <= 0) {
                System.out.println("Bitte Geben Sie eine Positive Zahl ein");
                IntervallScanner.next();

            }

        } catch (InputMismatchException e) {
            System.out.println("Bitte Geben Sie eine Zahl und keinen Buchstaben ein");
            IntervallScanner.next();

        }
    }

}

Wie gehts :)

Ich verstehe das nicht :/

 public static int prüfung(int ZahlPrüfen) {
  while (true) {
    try {

        int ZahlPrüfen = IntervallScanner.nextInt();
        if (IntervallScanner.hasNextInt() && ZahlPrüfen > 0) {
            return ZahlPrüfen;

        } else if (ZahlPrüfen <= 0) {
            System.out.println("Bitte Geben Sie eine Positive Zahl ein");
            IntervallScanner.next();
        }

    } catch (InputMismatchException e) {
        System.out.println("Bitte Geben Sie eine Zahl und keinen Buchstaben ein");
        IntervallScanner.next();

    }
}

You're passing in ZahlPrufen , but you replace it with IntervallScanner.nextInt(); . So the value passed in was never used.

You don't need the method to take in an argument.

The error lies in you checking for hasNextInt() in your if condition, which expects another input from the user. You just need to check the validity of the number that was just input.

public static int prüfung() {
while (true) {

    try {
    int ZahlPrüfen = IntervallScanner.nextInt();
    if (ZahlPrüfen > 0) {
        return ZahlPrüfen;

    } else {
        System.out.println("Bitte Geben Sie eine Positive Zahl ein");
    }

  } catch (InputMismatchException e) {
      System.out.println("Bitte Geben Sie eine Zahl und keinen Buchstaben ein");
        IntervallScanner.next();
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM