Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
2
Indexable
Never
import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double num1, num2, result;
        char operator;

        System.out.println("Calculator Java simplu");
        System.out.print("Introdu primul numar: ");
        num1 = scanner.nextDouble();

        System.out.print("Introdu operatorul (+, -, *, /): ");
        operator = scanner.next().charAt(0);

        System.out.print("Introdu al doilea numar: ");
        num2 = scanner.nextDouble();

        switch (operator) {
            case '+':
                result = num1 + num2;
                System.out.println("Rezultat: " + result);
                break;
            case '-':
                result = num1 - num2;
                System.out.println("Rezultat: " + result);
                break;
            case '*':
                result = num1 * num2;
                System.out.println("Rezultat: " + result);
                break;
            case '/':
                if (num2 != 0) {
                    result = num1 / num2;
                    System.out.println("Rezultat: " + result);
                } else {
                    System.out.println("Eroare: Impartire la zero!");
                }
                break;
            default:
                System.out.println("Operatorul introdus nu este valid.");
        }
    }
}