package workshop.tag6;

import java.util.Random;
import java.util.Scanner;

public class Gruppe6 {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner sc = new Scanner(System.in);

        int richtigeAntworten = 0;

        int runden = 10;

        System.out.println("Herzlich Willkommen! Dieses Spiel besteht aus " + runden + " Runden. Viel Glück!");
        for (int i = 0; i < runden; i++) {
            int x = rand.nextInt(101);
            boolean operator = rand.nextBoolean(); // true=+ , false=-
            int y;
            int richtigesErgebnis;
            if (operator) {
                y = rand.nextInt(100 - x);
                System.out.println(x + " + " + y + " =");
                richtigesErgebnis = x + y;
            } else {
                y = rand.nextInt(x + 1);
                System.out.println(x + " - " + y + " =");
                richtigesErgebnis = x - y;
            }

            int eingabe = readInteger(sc);

            if (eingabe == richtigesErgebnis) {
                richtigeAntworten++;
            }
        }
        System.out.println("Richtige Antworten: " + richtigeAntworten + " von " + runden);
    }

    public static int readInteger(Scanner sc) {
        int zahl;
        while (true) {
            System.out.print("Gib eine Zahl ein: ");
            try {
                zahl = Integer.parseInt(sc.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Das war kein Integer!");
            }
        }
        return zahl;
    }

}
