Created
July 24, 2023 17:02
-
-
Save john-nash-rs/99abb746d96d01a08296902ae42c2396 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class ArithmeticCalculator { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Enter the first number: "); | |
double num1 = scanner.nextDouble(); | |
System.out.print("Enter the second number: "); | |
double num2 = scanner.nextDouble(); | |
System.out.print("Enter the operation (+, -, *, /): "); | |
char operation = scanner.next().charAt(0); | |
double result = 0.0; | |
switch (operation) { | |
case '+': | |
result = num1 + num2; | |
break; | |
case '-': | |
result = num1 - num2; | |
break; | |
case '*': | |
result = num1 * num2; | |
break; | |
case '/': | |
if (num2 != 0) { | |
result = num1 / num2; | |
} else { | |
System.out.println("Error: Division by zero is not allowed."); | |
return; | |
} | |
break; | |
default: | |
System.out.println("Error: Invalid operation."); | |
return; | |
} | |
System.out.println("The result is: " + result); | |
scanner.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment