Computer Applications
Convert the following if else if construct into switch case:
if (a == 10)
System.out.println("TEN");
else if (a == 50)
System.out.println("FIFTY");
else if (a == 100)
System.out.println("HUNDRED");
else
System.out.println("UNKNOWN");
Java Conditional Stmts
27 Likes
Answer
switch (a) {
case 10:
System.out.println("TEN");
break;
case 50:
System.out.println("FIFTY");
break;
case 100:
System.out.println("HUNDRED");
break;
default:
System.out.println("UNKNOWN");
}
Answered By
9 Likes
Related Questions
Ravi runs the following Java code but encounters an error. Identify the statement causing the issue, correct it, and ensure the output is "Hungry".
char h = 'Y'; if (h == 'y' || 'Y') System.out.println("Hungry"); else System.out.println("Not Hungry");
Rewrite the following code using single if else statement:
if (brd.equals("ICSE")) { if (code == 86) { System.out.println("ICSE Computer Applications"); } else { System.out.println("Unknown subject"); } } else { System.out.println("Unknown subject"); }
A student executes the following program segment and gets an error. Identify the statement which has an error, correct the same to get the output as WIN.
boolean x = true; switch(x) { case 1: System.out.println("WIN"); break; case 2: System.out.println("LOOSE"); }
A triangle is said to be an 'Equable Triangle', if the area of the triangle is equal to its perimeter. Write a program to enter three sides of a triangle. Check and print whether the triangle is equable or not.
For example, a right angled triangle with sides 5, 12 and 13 has its area and perimeter both equal to 30.