Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- //yung class name is depende kung anong filename mo
- public class Samson_Calculator {
- static void calcu(){
- Scanner sc = new Scanner(System.in);
- int a, b, ans;
- char operator;
- System.out.print("Enter First Number: ");
- a = sc.nextInt();
- System.out.print("Enter Second Number: ");
- b = sc.nextInt();
- System.out.print("Enter Operator (+, -, *, /): ");
- operator = sc.next().charAt(0);
- switch(operator){
- case '+':
- ans = a+b;
- System.out.println(a+" + "+b+" = "+ans);
- break;
- case '-':
- ans = a-b;
- System.out.println(a+" - "+b+" = "+ans);
- break;
- case '*':
- ans = a*b;
- System.out.println(a+" * "+b+" = "+ans);
- break;
- case '/':
- ans = a/b;
- System.out.println(a+" / "+b+" = "+ans);
- break;
- default:
- System.out.println("Invalid Operator.");
- break;
- }
- }
- public static void main(String[] args) throws Exception{
- try{
- calcu();
- }catch(InputMismatchException e){
- System.out.println("Invalid Data Type. Please Enter a Number.");
- }catch(ArithmeticException er){
- System.out.println(er.getMessage());
- }finally{
- System.out.println("Try Catch Block Executed Successfully...");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment