+ 1

Why it is not running?

public class Program { public static void main(String[] args) { String buttons = "buttons"; switch(buttons) { case 2, 5, 8 -> "button1"; case 5, 3, 9 -> "button2"; default -> "invalid buttons!"; }; } }

29th Oct 2021, 3:51 PM
Mayukh Banerjee
Mayukh Banerjee - avatar
8 Respuestas
+ 1
The error is in String buttons first define buttons variable then pass in switch case statement
31st Oct 2021, 2:38 PM
yohana erick
yohana erick - avatar
+ 3
It should be String buttons;
29th Oct 2021, 4:02 PM
Альфья
Альфья - avatar
+ 2
Not String buttons=" buttons";
29th Oct 2021, 4:03 PM
Альфья
Альфья - avatar
+ 1
The issue is in String buttons first define buttons variable then pass in switch case
29th Oct 2021, 3:53 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Now also showing error
29th Oct 2021, 3:57 PM
Mayukh Banerjee
Mayukh Banerjee - avatar
+ 1
You are passing string type value to switch but cases using of int type so not matches. Error And you are not using or catching switch returned values so it can't output anything after removing error also. So print strings instead of just returning ... ( eg: "button 1" is incomplete statement) @ also using 5 as dublicate values for cases.. Dublicate cases not allowed.. Hope it helps to correct it..
29th Oct 2021, 4:21 PM
Jayakrishna 🇮🇳
+ 1
Do you mean System. Out. Println("Button1");
29th Oct 2021, 5:22 PM
Mayukh Banerjee
Mayukh Banerjee - avatar
0
Yes. Mayukh Banerjee that also valid. works...
29th Oct 2021, 5:33 PM
Jayakrishna 🇮🇳