Skip to content

Instantly share code, notes, and snippets.

@dheshanm
Created September 8, 2015 15:07
Show Gist options
  • Select an option

  • Save dheshanm/8509b1900fe40a30cd68 to your computer and use it in GitHub Desktop.

Select an option

Save dheshanm/8509b1900fe40a30cd68 to your computer and use it in GitHub Desktop.
Simple calculator
package starting.java.tutorials;
import java.util.Scanner;
public class Calculator {
public static void main(String Args[]){
Scanner in=new Scanner(System.in);
double n1,n2,n3;
System.out.println("Choose the Function to Perform");
String exec;
exec=in.next();
switch(exec)
{
case ("*"):
System.out.println("Enter the Numbers to Multiply");
n1=in.nextDouble();
n2=in.nextDouble();
n3=n1*n2;
System.out.println("The Result is "+n3);
break;
case "/":
System.out.println("Enter the Numbers to Divide");
n1=in.nextDouble();
n2=in.nextDouble();
n3=n1/n2;
System.out.println("The Result is "+n3);
break;
case "+":
System.out.println("Enter the Numbers to Add");
n1=in.nextDouble();
n2=in.nextDouble();
n3=n1+n2;
System.out.println("The Result is " +n3);
break;
case "-":
System.out.println("Enter the Numbers to Subtract");
n1=in.nextDouble();
n2=in.nextDouble();
n3=n1-n2;
System.out.println("The Result is "+n3);
break;
default:
System.out.println("You Chose the wrong option");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment