Advertisement
vvccs

E9_Calculator_Without_Swing

Nov 8th, 2023 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Calculator {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         System.out.println("Enter the first number: ");
  8.         double num1 = scanner.nextDouble();
  9.  
  10.         System.out.println("Enter the second number: ");
  11.         double num2 = scanner.nextDouble();
  12.  
  13.         System.out.println("Enter the operation (+, -, *, /): ");
  14.         char operation = scanner.next().charAt(0);
  15.  
  16.         double result;
  17.  
  18.         switch (operation) {
  19.             case '+':
  20.                 result = num1 + num2;
  21.                 System.out.println("Result: " + result);
  22.                 break;
  23.             case '-':
  24.                 result = num1 - num2;
  25.                 System.out.println("Result: " + result);
  26.                 break;
  27.             case '*':
  28.                 result = num1 * num2;
  29.                 System.out.println("Result: " + result);
  30.                 break;
  31.             case '/':
  32.                 result = num1 / num2;
  33.                 System.out.println("Result: " + result);
  34.                 break;
  35.             default:
  36.                 System.out.println("Invalid operation.");
  37.         }
  38.  
  39.         scanner.close();
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement