Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Switch statement question

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Switch statement question

    Hi guys . what's up

    Actually I have a question concernig switch statement that I tried to solve but I couldn't

    The question is :

    Write a program using switch case statement that simulates a calculator and that program will provide the basic integer arithmetic operations such as +, -, *, and /. The output of the program is shown as below:


    Enter first number :4
    Enter second number :5
    CHOICE OF CALCULATION
    '+' :plus
    '-' :Minus
    '*' :Multiply
    '/' :division
    Enter your choice :*
    The answer is 20
    Last edited by shikh_albelad; May 30th, 2009 at 10:27 AM.


  2. #2
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: Switch statement question

    Quick search in google gave me this

    The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    In the example above they switch an integer. In your case I would suggest creating char and switch it:
    int firstNumber;
    int secondNumber;
    char operation;
    //TODO: read character from the input
    switch(operation) {
        case '+':
            System.out.println("The answer is" + (firstNumber + secondNumber));
            break;
        case '-':
            System.out.println("The answer is" + (firstNumber - secondNumber));
            break;
        default:
            System.out.println("Unknown operator used..");
            break;
    }
    Last edited by Dalisra; May 30th, 2009 at 04:48 PM. Reason: Fixed quotation marks for the char

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Switch statement question

    Actually I tried the code it didn't work properly

  4. #4
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: Switch statement question

    You asked how does switch operation work.
    I explained it to you. However there are things you need to do yourself, you need to read firstNumber, secondNumber and operation character before you switch. I didn't do your whole asignement because it's YOURS job to do.

    Take a look at this SwitchExample class I made.
    public class SwitchExample {
     
    	public static void main(String[] args){
    		int firstNumber = 2;
    		int secondNumber = 1;
     
    		//you can change operation to + or -
    		char operation = '+';
     
    		switch(operation) {
    		    case '+':
    		        System.out.println("The answer is " + (firstNumber + secondNumber));
    		        break;
    		    case '-':
    		        System.out.println("The answer is " + (firstNumber - secondNumber));
    		        break;
    		    default:
    		        System.out.println("Unknown operator used..");
    		        break;
    		}
    	}
     
    }
    Last edited by Dalisra; May 31st, 2009 at 04:43 AM.

  5. The Following User Says Thank You to Dalisra For This Useful Post:

    JavaPF (June 1st, 2009)

  6. #5
    Junior Member
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Talking Re: Switch statement question

    Thaaanks for your help. And sorry for disturbing you Delisra, you know I'm still new in java progrmming



  7. #6
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: Switch statement question

    I hope it works now!

  8. The Following User Says Thank You to Dalisra For This Useful Post:

    shikh_albelad (May 31st, 2009)

Similar Threads

  1. Getting an error while altering a source code
    By marksquall in forum Collections and Generics
    Replies: 3
    Last Post: June 8th, 2009, 03:49 AM
  2. [SOLVED] How to narrow down the range of input in java?
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2009, 12:38 PM
  3. How to show 2D array using combobox and radiobutton?
    By Broken in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2009, 07:01 AM