Results 1 to 3 of 3

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

  1. #1
    Join Date
    Oct 2015
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,035
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    If this is for your school assignment, you should probably seek help from your classmates or tutor/teacher.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    432
    Thanks
    0
    Thanked 54 Times in 50 Posts

    Default

    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.

Similar Threads

  1. Resolved switch statement with no page found option
    By ?foru in forum PHP
    Replies: 7
    Last Post: 03-08-2014, 04:38 PM
  2. Replies: 6
    Last Post: 09-08-2013, 01:02 AM
  3. Change syntax from Update Case Statement to Update Union Statement
    By newphpcoder in forum MySQL and other databases
    Replies: 0
    Last Post: 11-11-2011, 01:23 AM
  4. Replies: 4
    Last Post: 12-16-2005, 06:44 PM

Bookmarks

Posting Permissions

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