import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number:");
double num1 = sc.nextDouble();
System.out.println("Enter second number:");
double num2 = sc.nextDouble();
System.out.println("Select operation (+, -, *, /):");
char operation = sc.next().charAt(0);
double result;
switch (operation) {
case '+':
result = num1 + num2;
System.out.println("Result: " + result);
break;
case '-':
result = num1 - num2;
System.out.println("Result: " + result);
break;
case '*':
result = num1 * num2;
System.out.println("Result: " + result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
System.out.println("Result: " + result);
} else {
System.out.println("Error: Cannot divide by zero.");
}
break;
default:
System.out.println("Error: Invalid operation.");
}
sc.close();
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter