Results 1 to 6 of 6
Like Tree1Likes

Thread: Switch case ...interseting problem

  1. #1
    kishor123 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Default Switch case ...interseting problem

    hi i am currently designing a calculator in java and i have a code here that is to handle this code i use if-else statement
    if (decision == 1)
    {
    Multiplication();
    }
    if (decision == 2)
    {
    Addition();
    }
    if (decision == 3)
    {
    Subtraction();
    }
    if (decision == 4)
    {
    Division();
    }
    But now i want to use same program using switch statement.
    i.e using switch case in place of if statement here...
    can anyone please help...???


    Thanks in advance

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Switch case ...interseting problem

    The page on the switch statement in Oracle's Tutorial begins with a comparison of switch and a chain of if statements.

    Post the code you are using with switch if you get stuck applying what's shown there.

  3. #3
    kishor123 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Default Re: Switch case ...interseting problem

    Quote Originally Posted by pbrockway2 View Post
    The page on the switch statement in Oracle's Tutorial begins with a comparison of switch and a chain of if statements.

    Post the code you are using with switch if you get stuck applying what's shown there.
    ok i will try it ...


    Thanks for Qick Support

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Switch case ...interseting problem

    You're welcome.

    Most people here don't like writing code for others, but if you have any problems at all with what's in the Tutorial you should post back because someone is sure to be able to help.

  5. #5
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: Switch case ...interseting problem

    These two snippets of code do exactly the same thing. Just follow this format and it is pretty easy. Just remember that if you don't add the break, it could execute more then one piece of the switch statement; and don't forget to add the default at the bottom.

    Java Code:
            String str;
            
            System.out.println("Enter a number");
            int var = keyboard.nextInt();
            
            if(var == 1)
            	str = "one";
            else if(var == 2)
            	str = "two";
            else if(var == 3)
            	str = "three";
            else if(var == 4)
            	str = "four";
            else 
            	str = "greater than four";
    
            System.out.println(str);
    Java Code:
            String str;
            
            System.out.println("Enter a number");
            int var = keyboard.nextInt();
    
            switch (var) {
            	case 1:  str = "one";
            		break;
            	case 2:  str = "two";
            		break;
            	case 3:  str = "three";
            		break;
            	case 4:  str = "four";
            		break;
            	default: str = "greater than four";
            		break;
            }
            
            System.out.println(str);
    Last edited by penguinCoder; 10-09-2012 at 06:18 PM. Reason: added curly brace

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. about conditonal if else and switch case
    By niksipandit in forum New To Java
    Replies: 6
    Last Post: 09-22-2011, 12:53 PM
  2. java switch case
    By aconti in forum New To Java
    Replies: 16
    Last Post: 08-09-2011, 08:05 AM
  3. Converting if to Switch Case.
    By king2be98 in forum New To Java
    Replies: 5
    Last Post: 02-20-2011, 04:46 PM
  4. Switch Case statement
    By seanfmglobal in forum New To Java
    Replies: 7
    Last Post: 02-15-2011, 02:18 PM
  5. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 06:56 PM

Posting Permissions

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