# Thread: how to convert from If/If Else to Switch Statement

1. New Comer (less than 5 posts)
Join Date
Oct 2015
Posts
1
Thanks
0
Thanked 0 Times in 0 Posts

## how to convert from If/If Else to Switch Statement

I need to convert the if statement inside my program to a switch statement for a school assignment and I have no clue where to start or how to change it. Please help me! sorry if I did this incorrectly I have never posted here before.

Code:
import java.util.Scanner;

public class hw_question_2{

public static void main(String[] args){

Scanner input=new Scanner(System.in);
double x, y;

System.out.println("The value for 'x' can be 7, 15, or 30");
System.out.println("Enter a value for 'x'");
x=input.nextDouble();

System.out.println("Enter a value for 'y'");
y=input.nextDouble();

if (x==7)
y=7.5;
else if (x==15)
y=8.50;
else if(x==30)
y=9.0;
else y=10.0;

System.out.print("The value for 'x' is: " +x +"\n");
System.out.print("The value for 'y' is: " +y +"\n");

}
}
Last edited by Beverleyh; 10-28-2015 at 08:03 AM. Reason: Formatting

2. If this is for your school assignment, you should probably seek help from your classmates or tutor/teacher.

3. Senior Coders
Join Date
Nov 2014
Location
On A Scottish Island
Posts
488
Thanks
0
Thanked 62 Times in 58 Posts
As this appears to be a Java course that you're studying this page might help.

Incidentally, the "switch" statement syntax is common to C, C++, Java, JavaScript and PHP; so it's a good thing to learn.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•