3-3 CalculatorSwitch

posted Jan 29, 2014, 7:15 AM by Dlovan ismail   [ updated Jan 29, 2014, 7:15 AM by Karwan Jacksi ]
// CalculatorSwitch
// Simple arithmetic calculator using switch() selection.

package calculatorswitch;
import java.util.Scanner;

public class CalculatorSwitch 
{
    public static void main(String[] args)
    {
           //-------defining variables and initializing them------------- 
             double num1,num2;
             char operation;

             //-------defining a class Scanner for inputting data------------- 
            Scanner input=new Scanner(System.in);

            //--------Printing my name on screen----------------  
            System.out.println("Welcome to the calculater program  written by Your Name");
            System.out.println("***************************************************************");

            System.out.print("Please enter an operation which you like to calculate (+,-,*,/) : ");
            operation=input.next().charAt(0);

            System.out.println("Please enter two numbers to apply your requested operation("+operation+"):");

            System.out.print("ist num : ");
            num1=input.nextDouble();

            System.out.print("2st num : ");
            num2=input.nextDouble();

            //---used switch function so thet the operater can be decided------------ 
            switch(operation)
            {
                //------calculating the requested equation for inputs------------- 
                //-------at the same time printing the results on screen-----------
                case '+':
                    System.out.println("The addition of two numbers ("+num1+","+num2+"): "+(num1+num2));
                    break;
                case '-':
                    System.out.println("The substraction of two numbers ("+num1+","+num2+"): "+(num1-num2));
                    break;
                case '*':
                    System.out.println("The multiplication of two numbers ("+num1+","+num2+"): "+(num1*num2));
                    break;
                case '/':
                    if(num2!=0)
                        System.out.println("The division of two numbers ("+num1+","+num2+"): "+(num1/num2));
                    else
                        System.out.println("Can’t divided by zero");
                    break;
                default:
                    System.out.println("unknown command");
            }

    }  
}

ċ
CalculatorSwitch.java
(2k)
Karwan Jacksi,
Jan 29, 2014, 7:15 AM
Comments