From a43cebc636411a5e713661fa0bcd6cf6c4c4c519 Mon Sep 17 00:00:00 2001 From: Alexander Bachinger <139718933+AlexBa16@users.noreply.github.com> Date: Fri, 23 May 2025 15:29:11 +0200 Subject: [PATCH] UE22 --- src/UE22_200525_MatheServer/MatheClient.java | 175 ++++++++++--------- 1 file changed, 95 insertions(+), 80 deletions(-) diff --git a/src/UE22_200525_MatheServer/MatheClient.java b/src/UE22_200525_MatheServer/MatheClient.java index 45dfa8f..f36ed40 100644 --- a/src/UE22_200525_MatheServer/MatheClient.java +++ b/src/UE22_200525_MatheServer/MatheClient.java @@ -22,100 +22,115 @@ public class MatheClient extends Thread { @Override public void run() { try (BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream())); BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()))) { - wr.write("Willkommen beim Mathe-Quiz\n\rWie heftig hättest du es gerne?"); - wr.flush(); while (true) { - int level; - wr.write("\n\rBitte gibt eine Zahl zwischen 1 und 3 ein. (Leer für 1): "); - wr.flush(); - try { - String input = br.readLine(); - if (input == null || input.trim().isEmpty()) break; - level = Integer.parseInt(input); - } catch (Exception _) { - continue; - } - if (level < 1 || level > 3) continue; - this.level = level; - break; - } - while (true) { - int anz; - wr.write("\n\rWie viele Fragen meochtest du loesen (1 - 999) (Leer für 10): "); - wr.flush(); - try { - String input = br.readLine(); - if (input == null || input.trim().isEmpty()) break; - anz = Integer.parseInt(input); - } catch (Exception _) { - continue; - } - if (anz < 1 || anz > 999) continue; - this.anzCals = anz; - break; - } - for (int i = 1; i <= anzCals; i++) { - int type = randomInRange(1, 4), solution, a, b; - String operator; - switch (level) { - case 1 -> { - a = randomInRange(1, 10); - b = randomInRange(1, 10); - } - case 2 -> { - a = randomInRange(-100, 100); - b = randomInRange(-100, 100); - } - case 3 -> { - a = randomInRange(-10_000, 10_000); - b = randomInRange(-10_000, 10_000); - } - default -> throw new IllegalStateException("Unexpected value: " + level); - } - switch (type) { - case 1 -> { - operator = " + "; - solution = a + b; - } - case 2 -> { - operator = " - "; - solution = a - b; - } - case 3 -> { - operator = " * "; - solution = a * b; - } - case 4 -> { - operator = " / "; - solution = a / (b == 0 ? 1 : b); - } - default -> throw new IllegalStateException("Unexpected value: " + type); - } - String question = "(Level " + level + ") Frage " + i + ": " + a + operator + b + " = "; - wr.write(question); + wr.write("Willkommen beim Mathe-Quiz\n\rWie heftig hättest du es gerne?"); wr.flush(); while (true) { - String message = "\n\rFalsch :( .. versuche es noch einmal\n\n\r" + question; + int level; + wr.write("\n\rBitte gibt eine Zahl zwischen 1 und 3 ein. (Leer für 1): "); + wr.flush(); try { String input = br.readLine(); - if (input == null || input.trim().isEmpty() || Integer.parseInt(input) != solution) { + if (input == null || input.trim().isEmpty()) break; + level = Integer.parseInt(input); + } catch (Exception _) { + continue; + } + if (level < 1 || level > 3) continue; + this.level = level; + break; + } + while (true) { + int anz; + wr.write("\n\rWie viele Fragen meochtest du loesen (1 - 999) (Leer für 10): "); + wr.flush(); + try { + String input = br.readLine(); + if (input == null || input.trim().isEmpty()) break; + anz = Integer.parseInt(input); + } catch (Exception _) { + continue; + } + if (anz < 1 || anz > 999) continue; + this.anzCals = anz; + break; + } + for (int i = 1; i <= anzCals; i++) { + int type = randomInRange(1, 4), solution, a, b; + String operator; + switch (level) { + case 1 -> { + a = randomInRange(1, 10); + b = randomInRange(1, 10); + } + case 2 -> { + a = randomInRange(-100, 100); + b = randomInRange(-100, 100); + } + case 3 -> { + a = randomInRange(-10_000, 10_000); + b = randomInRange(-10_000, 10_000); + } + default -> throw new IllegalStateException("Unexpected value: " + level); + } + switch (type) { + case 1 -> { + operator = "+"; + solution = a + b; + } + case 2 -> { + operator = "-"; + solution = a - b; + } + case 3 -> { + operator = "*"; + solution = a * b; + } + case 4 -> { + operator = "/"; + solution = a / (b == 0 ? 1 : b); + } + default -> throw new IllegalStateException("Unexpected value: " + type); + } + String question = "(Level " + level + ") Frage " + i + ": " + a + " " + operator + " " + b + " = "; + wr.write(question); + wr.flush(); + while (true) { + String message = "\n\rFalsch :( .. versuche es noch einmal\n\n\r" + question; + try { + String input = br.readLine(); + if (input == null || input.trim().isEmpty() || Integer.parseInt(input) != solution) { + wr.write(message); + wr.flush(); + continue; + } + } catch (Exception _) { wr.write(message); wr.flush(); continue; } - } catch (Exception _) { - wr.write(message); + wr.write("\n\rrichtig :)\n\n\r"); wr.flush(); - continue; + break; } - wr.write("\n\rrichtig :)\n\n\r"); - wr.flush(); - break; } + wr.write("\n\n\n\rHerzlichen Glückwunsch du hast es geschafft!!\n"); + wr.flush(); + boolean end = true; + while (true) { + wr.write("\n\rMoechtest du noch einmal spielen? [y/N]: "); + wr.flush(); + String input = br.readLine(); + if (input == null || input.trim().isEmpty() || input.trim().equalsIgnoreCase("n")) break; + if (input.trim().equalsIgnoreCase("y")) { + end = false; + break; + } + } + if (end) break; } - wr.write("\n\n\n\rHerzlichen Glückwunsch du hast es geschafft!!\n\n\rENDE"); - wr.flush(); } catch (Exception _) { } + } } \ No newline at end of file