MeraForum Community.No 1 Pakistani Forum Community

MeraForum Community.No 1 Pakistani Forum Community (http://www.MeraForum.Com/index.php)
-   Java (http://www.MeraForum.Com/forumdisplay.php?f=217)
-   -   CLASS 4: Calculator Using Switch Statement In Java (http://www.MeraForum.Com/showthread.php?t=113520)

Morash 04-17-2014 01:01 AM

CLASS 4: Calculator Using Switch Statement In Java
 


Calculator In Java Using Switch Statement




The switch Statement Is Used As A Reciprocal Of nested if else Statements. It Is Used When Multiple Choices Are Given And One Choice Is To Be Selected.
The 'nested if-else' Structure Becomes Complicated In Multiple Choices So Then We Used switch Statement.
One Of The Choices Or Cases In The switch Statement Is Executed Depending Upon The Value Returned By The Expression.

In The switch Statement The break Statement Is Normally Used At The End Of Statements In Each Case. It Exits The Control From The Body Of switch Structure.
If It Is Not Used Then The Statements Of Other Cases That Come After The Matching Case Will Also Be Executed.


Syntax

switch(expression)
{
case constant:
statement;
break;
}




Example Of Making A Calculator


http://i57.tinypic.com/2rh1lzb.jpg




Output


Addition

http://i60.tinypic.com/2uf3re1.jpg



Subtraction

http://i61.tinypic.com/2cpumj9.jpg


Multiplication

http://i58.tinypic.com/24p02l2.jpg


Division

http://i58.tinypic.com/2iviipw.jpg



pakeeza 04-17-2014 10:19 AM

Re: CLASS 4: Calculator Using Switch Statement In Java
 
Gooo shaarIng

AYAZ 04-19-2014 07:09 PM

Re: CLASS 4: Calculator Using Switch Statement In Java
 
This is great share morash, I will try this and will let you know if I have any question about it

Akash 04-19-2014 11:12 PM

Re: CLASS 4: Calculator Using Switch Statement In Java
 
nice sharing bro da java ba za sta na yadom khu ma di rab tek no. zama tol fan chi sok di salamona warla waya

AYAZ 04-20-2014 08:32 PM

Re: CLASS 4: Calculator Using Switch Statement In Java
 
Morash check this code, It's giving me error don't know why

Code:

package cal;
import java.util.Scanner;

/**
 *
 * @author Ayaz
 */
public class cal
{

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        // TODO code application logic here
        float num1 , num2;
String operations;

Scanner input = new Scanner (System.in);
System.out.print("Enter the First number");
num1 = input.nextFloat();

System.out.print("Enter the operation");
operations = input.next();

System.out.print("Enter the Second Number");
num2= input.nextFloat();

switch (operations){
   
   
    case "+":
    System.out.print("Addition of "+num1+"and "+num2+" is =  "+ (num1 + num2));
    break;

    case "-":
    System.out.print("Substraction of "+num1+"and "+num2+" is = "+ (num1 - num2));
    break;
   
    case "/":
    System.out.print ("Division of "+num1+" and "+num2+" is" = + (num1 / num2));
    break;

    case "*":
    System.out.print("Multiplication  of "+num1+" and "+num2+" is" = + (num1 * num2));
    break;


   

  }
    }}


Morash 04-22-2014 11:05 PM

Re: CLASS 4: Calculator Using Switch Statement In Java
 
Quote:

Originally Posted by AYAZ (Post 1935095)
Morash check this code, It's giving me error don't know why

Code:

package cal;
import java.util.Scanner;

/**
 *
 * @author Ayaz
 */
public class cal
{

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        // TODO code application logic here
        float num1 , num2;
String operations;

Scanner input = new Scanner (System.in);
System.out.print("Enter the First number");
num1 = input.nextFloat();

System.out.print("Enter the operation");
operations = input.next();

System.out.print("Enter the Second Number");
num2= input.nextFloat();

switch (operations){
   
   
    case "+":
    System.out.print("Addition of "+num1+"and "+num2+" is =  "+ (num1 + num2));
    break;

    case "-":
    System.out.print("Substraction of "+num1+"and "+num2+" is = "+ (num1 - num2));
    break;
   
    case "/":
    System.out.print ("Division of "+num1+" and "+num2+" is" = + (num1 / num2));
    break;

    case "*":
    System.out.print("Multiplication  of "+num1+" and "+num2+" is" = + (num1 * num2));
    break;


   

  }
    }}


Your Package And Cal Class Names Are Same, Change cal Class Name To Cal (Capital C)
Last 2 Jo System.out.print K Statements Hai Os Mai is K Saath Jo Double Commas Lage Hai, Wo Remove Karo And Equal Sign K Baad Lagana ..

AYAZ 04-23-2014 02:23 AM

Re: CLASS 4: Calculator Using Switch Statement In Java
 
Thanks morash.


All times are GMT +5. The time now is 04:52 PM.

Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.