Question

In: Computer Science

Convert the following switch statement into if-else statements: int month = input.nextInt(); switch (month) { case...

Convert the following switch statement into if-else statements:

int month = input.nextInt();
switch (month) {

case 1: System.out.println(“January”);

case 2: System.out.println(“February”); break;

case 3: System.out.println(“March”); break;

case 4: System.out.println(“April”);

case 5: System.out.println(“May”); break;

default: System.out.println(“Invalid”); break;

}

example:

if (month == 1) {

System.out.println("January");

System.out.println("February"); }
if (month == 2)
{ System.out.println("February");
  
}
}
}

Please continue the code! Thanks

Solutions

Expert Solution

int month = input.nextInt();
switch (month) {

case 1: System.out.println(“January”);

case 2: System.out.println(“February”); break;

case 3: System.out.println(“March”); break;

case 4: System.out.println(“April”);

case 5: System.out.println(“May”); break;

default: System.out.println(“Invalid”); break;

}

The Above Switch statement has been converted into if-else statements i.e the else- if statements.

import java.util.*;
public class Months
{
    public static void main(String[] args)
    {
    Scanner input=new Scanner(System.in);
        System.out.println("Enter the month in terms of number");
        int month=input.nextInt();//Getting input from user
        if(month==1)//if the condition is true,the statements inside the if statemnent gets executed.
        {
            System.out.println("January");
            System.out.println("February");
        }
        else if(month==2)
        {
            System.out.println("February");
        }
        else if(month==3)
        {
            System.out.println("March");
        }
        else if(month==4)
        {
            System.out.println("April");
            System.out.println("May");
        }
        else if(month==5)
        {
            System.out.println("May");
        }
        else //if none of the conditions are true,then else statement gets executed.
        {
            System.out.println("Invalid");
        }
    }
}

Screenshot of the program with the output is given below :

Output is March,if the user inputs the value as 3,the output will be displayed as March.


Related Solutions

Re-write following if-else-if statements as Switch statement. Your final code should result in the same output...
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); 2. Write a Constructor for the TrafficLight class that sets stopLight value to “red”, waitLight to “yellow” and goLight to “green”?
consider execution of the following switch statement: int Enter = 10; cin >> Enter; switch (Enter)...
consider execution of the following switch statement: int Enter = 10; cin >> Enter; switch (Enter) { case 1: Enter = -4; case 2: Enter = -6; case 4: break; case 6: Enter = -8; break; default: Enter = -1; } What would the value of Enter be after execution of this code if the value read for Enter were 4? -4,-6,-8 or none
Analysis and Discussion about if, if else, else if statement and Switch C programming write on...
Analysis and Discussion about if, if else, else if statement and Switch C programming write on your computer font
As it relates to C#: Though the function of switch case and else if ladder is...
As it relates to C#: Though the function of switch case and else if ladder is same, there are a number of  difference between switch case and else if ladder, Explain the differences in two areas such (memory consumption, speed of processing, variable requirement) etc.
in c++ codeblocks Convert the following code into a switch structure. Make the switch structure as...
in c++ codeblocks Convert the following code into a switch structure. Make the switch structure as short as possible (do not repeat code). if (i == 3 || i == 5) { n++; tryagain = 0; } else if (i == 4) || i == 10) { n = 5; } else if (i == 6) { n = 6; } else { n = 0; tryagain = 1; } Write an if statement that will give 5 extra credit...
C language <stdio.h> (Decisions only) (else if and Switch statements) A bank wants an application to...
C language <stdio.h> (Decisions only) (else if and Switch statements) A bank wants an application to help in the loan approval process and has asked us to create it for them. Write a program to ask the user for the loan amount, the annual interest rate (as a percentage, not as a decimal), the length of the loan (in years), and the gross monthly income of the applicant. Validate the inputs per below, and if invalid, inform the user and...
Convert the following C++ statements to an ARM assembly language program: const int size = 10;...
Convert the following C++ statements to an ARM assembly language program: const int size = 10; int x[size] = {8, 2, 9, 6, 7, 0, 1, 3, 5, 4}; int y[size] = {399, -87, 12, 0, 42, -367, 57, 92, -1000, 25}; for i = 0; i < size; i++) if (x([ i ] > y[ i ]) z[ i ] = 0 else z[ i ] = 1;
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT