Whats wrong with my code (from lection 2 Java) | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Whats wrong with my code (from lection 2 Java)

My code is : import java.util.Scanner; public class Main { public static void main(String[] args) { do { Scanner scanner = new Scanner(System.in); int eingabe = scanner.nextInt(); switch(eingabe){ case 1: System.out.println("Language selection");break; case 2: System.out.println("Customer support");break; case 3: System.out.println("Check the balance");break; case 4: System.out.println("Check loan balance");break; case 0: System.out.println("Exit");break; } } while(eingabe != 0); } } But I really dont Know what I did wrong there. It says me : Cannot find symbol when I wanna compile..

22nd Dec 2020, 2:51 PM
Kevin S
Kevin S - avatar
5 ответов
+ 5
The variable "eingabe" is declared inside the do { } and thus not accessible outside it that is in the while(eingabe ...) So declare eingabe as an integer before the do while loop . [Always beware of the scope of variables]
22nd Dec 2020, 3:06 PM
Alphin K Sajan
Alphin K Sajan - avatar
+ 3
Need to move the Scanner definition out of the do-while loop also Scanner scanner = new Scanner(System.in); // move this out of do-while loop
22nd Dec 2020, 3:28 PM
Ipang
+ 1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int eingabe =0; eingabe = scanner.nextInt(); do { switch(eingabe){ case 1: System.out.println("Language selection");break; case 2: System.out.println("Customer support");break; case 3: System.out.println("Check the balance");break; case 4: System.out.println("Check loan balance");break; case 0: System.out.println("Exit");break; } eingabe = scanner.nextInt(); } while(eingabe != 0); } }
22nd Dec 2020, 6:31 PM
Maharaja
+ 1
I solved it - thanks to all of you!
22nd Dec 2020, 6:45 PM
Kevin S
Kevin S - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { int eingabe; do { Scanner scanner = new Scanner(System.in); eingabe = scanner.nextInt(); switch(eingabe){ case 1: System.out.println("Language selection");break; case 2: System.out.println("Customer support");break; case 3: System.out.println("Check the balance");break; case 4: System.out.println("Check loan balance");break; case 0: System.out.println("Exit");break; } } while(eingabe != 0); } } I edited it now. But its still not working as intended , only when the initial inout is 0 its working.. :/
22nd Dec 2020, 3:12 PM
Kevin S
Kevin S - avatar