I am attempting to change this switch to a boolean. any help would be appreciate
ID: 3699244 • Letter: I
Question
I am attempting to change this switch to a boolean. any help would be appreciated! plastic = 'c' , waffle = 'w', sugar = 's', and souvenir = 'b' switch (ContainerOption) { // edit here cannot do switch try boolean or if else case 1: ContainerName = "plastic cup"; System.out.printf("%-18s%s ", "Container: ", ContainerName); break; case 2: ContainerName = "sugar cone"; System.out.printf("%-20s%s ", "Container: ", ContainerName); break; case 3: ContainerName = "waffle cone"; System.out.printf("%-19s%s ", "Container: ", ContainerName); break; case 4: ContainerName = "souvenir bowl"; System.out.printf("%-17s%s ", "Container: ", ContainerName); break; case 5: ContainerName = "Invalid choice"; System.out.printf("%-18s%s ", "Container: ", "plastic cup");
Explanation / Answer
I used if .. else statement here instead of switch here.
Code:
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int ContainerOption=sc.nextInt();
String ContainerName;
if(ContainerOption==1){
ContainerName = "plastic cup";
System.out.printf("%-18s%s ", "Container: ", ContainerName);
}
else if(ContainerOption==2){
ContainerName = "sugar cone";
System.out.printf("%-20s%s ", "Container: ", ContainerName);
}
else if(ContainerOption==3){
ContainerName = "waffle cone";
System.out.printf("%-19s%s ", "Container: ", ContainerName);
}
else if(ContainerOption==4){
ContainerName = "souvenir bowl";
System.out.printf("%-17s%s ", "Container: ", ContainerName);
}
else {
ContainerName = "Invalid choice";
System.out.printf("%-18s%s ", "Container: ", "plastic cup");
}
}
}
Please like it if it helped.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.