Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
892 B
1
Indexable
Never
package lab_2;
import java.util.Scanner;
public class calculator {
	


	    public static void main(String[] args) {
	        Scanner scanner = new Scanner(System.in);

	        System.out.print("Enter two numbers: ");
	        double num1 = scanner.nextDouble();
	       
	    
	        double num2 = scanner.nextDouble();

	        System.out.print("Enter operation (+, -, *, /): ");
	        char operation = scanner.next().charAt(0);

	        double result = switch (operation) {
	            case '+' -> num1 + num2;
	            case '-' -> num1 - num2;
	            case '*' -> num1 * num2;
	            case '/' -> (num2 != 0) ? num1 / num2 : Double.NaN;
	            default -> {
	                System.out.println("Invalid operation.");
	                yield 0;
	            }
	        };

	        System.out.println("Result: " + result);

	        scanner.close();
	    }
	}


Leave a Comment