From f47b3a3ed75ecf243b5b0a8a1d8ab1d6e78e4b61 Mon Sep 17 00:00:00 2001 From: Eric Paci <3014947@stud.hs-mannheim.de> Date: Tue, 14 May 2024 22:27:03 +0200 Subject: [PATCH] =?UTF-8?q?first=20draft=20+=20methode=20f=C3=BCr=20eingab?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sonderübungen/src/Schachbrett.java | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Sonderübungen/src/Schachbrett.java b/Sonderübungen/src/Schachbrett.java index 4d35f77..480096d 100644 --- a/Sonderübungen/src/Schachbrett.java +++ b/Sonderübungen/src/Schachbrett.java @@ -7,6 +7,16 @@ public class Schachbrett { public Schachbrett() { felder = new String[][] {{"A","B","C","D","E","F","G","H"},{"1","2","3","4","5","6","7","8"}}; + } + + public static int gueltigeEingabe(Scanner in, String achse) { + int zahl = 0; + do { + System.out.println("Position auf " + achse + " Achse: "); + zahl = in.nextInt(); + } while (istEingabeUngueltig(zahl)); + + return zahl; } public static boolean istEingabeUngueltig(int Zahl) { @@ -26,19 +36,8 @@ public class Schachbrett { public static void main(String[] args) { Scanner in = new Scanner(System.in); - int x = 0; - int y = 0; - - do { - System.out.println("Position auf x Achse: "); - x = in.nextInt(); - } while (istEingabeUngueltig(x)); - - do { - System.out.println("Position auf y Achse: "); - y = in.nextInt(); - } while (istEingabeUngueltig(y)); - + int x = gueltigeEingabe(in, "x"); + int y = gueltigeEingabe(in, "y"); in.close(); Schachbrett instanz = new Schachbrett();