0

it kepps giving me 1 error for my switch statement that is a value to a string

public class Program { public static void main(String[] args) { String animal="cat"; String yes = "true"; String tomato = "fruit "; String rock = " strong"; String small = "dogs"; String two = tomato + rock; boolean cuff = true; String domain = switch(small) { case "donkey": System.out.println("te"); case "rat": System.out.println("no"); case "panda": System.out.println("yes"); case "cat": System.out.println("today"); case "dogs": System.out.println("present moment"); } ; switch(animal){ case "donkey": System.out.println("te"); break; case "rat": System.out.println("no"); break; case "panda": System.out.println("yes"); break; case "cat": System.out.println("today"); break; case "elephant": System.out.println("present moment"); break; } switch (yes){ case "fale": System.out.println(rock); break; case "no": System.out.println(tomato); break; case "true": System.out.println(two); break; case "yes": System.out.println("no not today"); break; } if(2<4){ System.out.println(two); if(34==34) System.out.println(tomato); if(0>4) System.out.println("no"); else System.out.println("have a good day"); } if(5==5&&2<3){ System.out.println(domain); } } }

22nd Feb 2022, 9:19 AM
Raja Velu
Raja Velu - avatar
3 Respostas
+ 1
Raja Velu What you are trying to achieve? Make code in Code Playground and share the link with question.
22nd Feb 2022, 9:22 AM
A͢J
A͢J - avatar
0
Manav Roy In his case he is trying to assign switch case to a variable domain which is not possible in old java version. In Java 11 you can assign but you need to use another syntax. This is Java 11 syntax String abc = switch (type) { case 1 -> "abc"; case 2 -> "def"; default -> "none"; } This is before java 11: switch (type) { case 1: //abc break; case 2: //def break; default: break; } Raja Velu if you want to assign switch case value to a variable then make a method with return type of String. Now return switch value in method then assign returned value to a variable. You can also assign printed value to domain variable but you cannot assign switch to a variable https://code.sololearn.com/c30BKx8LvVPe/?ref=app
23rd Feb 2022, 11:48 AM
A͢J
A͢J - avatar