Saturday, November 1, 2014

Switch case java pro



Switch case statement for calculator
 
import java.io.*;
class calculatorEx
{
public calculatorEx()
 {
      System.out.println("constructor invoked");
}
 public void calc()
 { 
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               int a, b,c, d,ch;
               try
               {
               
               System.out.println("Enter the value of a");
               a = Integer.parseInt(br.readLine());
               System.out.println("Enter the value of b");
               b = Integer.parseInt(br.readLine());
               System.out.println("Enter the value of c ");
               c = Integer.parseInt(br.readLine());
               System.out.println("\n-----Main Menu-----\n");
               System.out.println("1.Addition");
               System.out.println("2.Multiplication");
               System.out.println("3. Subtraction");
               System.out.println("4. Average");
               System.out.println("------------------------------------------------------------------------");
               System.out.println("\nEnter the function which u want to perform\n");
               ch = Integer.parseInt(br.readLine());
                 switch(ch)
               {
                   case '1':
                   d = a + b + c;
                   System.out.println("The result is " + d);
                   break;
                  case '2' :
                   d = a * b * c;
                   System.out.println("The result is " + d);
                   break;
                   case '3' :
                    d = a - b - c;
                    System.out.println(" The result is" + d);
                     break;
                     case '4':
                    d = a + b + c/2;
                    System.out.println("The result is " + d);
                    break;
                    default:
                    System.out.println(" Oops! wrong choice");
                        break;
                     } 
               }
               catch(IOException ca)
               {
                   System.out.println("Exception caught" + ca);
               }
         }
               public static void main(String ar[])
              {
                   calculatorEx calc1 = new calculatorEx();
                   calc1.calc();
        }
}

A simple java calculator based on switch case

public class Cal
{
public static void main(String args[])throws Exception
{
int a,b,c=0;
BufferedReader br=new BufferedReader(new InputStreanreader(System.in));
System.out.println(“Enter 1st number”);
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
System.out.println(“\nMENU\n1.Add\n2.Subtract\n3.Multiply\n4.Devide\nEnter your choice:”);
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1: c=a+b;
break;
case 2:c=a-b;
break;
case 3:c=a*b;
break;
case 4:c=a/b;
break;
default: System.out.println(“Wrong Choice”);
}
System.out.println(c);
}
}

No comments:

Post a Comment