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
Which of the following is true about the
default
label in aswitch
statement?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");
Which of the following data type cannot be used with switch case construct?
- int
- char
- String
- double
Write a program to input three numbers (positive or negative). If they are unequal then display the greatest number otherwise, display they are equal. The program also displays whether the numbers entered by the user are 'All positive', 'All negative' or 'Mixed numbers'.
Sample Input: 56, -15, 12
Sample Output:
The greatest number is 56
Entered numbers are mixed numbers.