import java.util.Scanner;
public class Taschenrechner {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean continueCalculations = true;
while (continueCalculations) {
System.out.println("Bitte wählen Sie eine Rechenoperation:");
System.out.println("1. Plus (+)");
System.out.println("2. Minus (-)");
System.out.println("3. Mal (*)");
System.out.println("4. Geteilt (/)");
System.out.println("5. Fakultät (!)");
System.out.println("6. Quadrat (^2)");
System.out.println("7. Wurzel (√)");
System.out.println("8. Beenden");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Geben Sie die erste Zahl ein:");
double addend1 = scanner.nextDouble();
System.out.println("Geben Sie die zweite Zahl ein:");
double addend2 = scanner.nextDouble();
System.out.println("Ergebnis: " + (addend1 + addend2));
break;
case 2:
System.out.println("Geben Sie die erste Zahl ein:");
double minuend = scanner.nextDouble();
System.out.println("Geben Sie die zweite Zahl ein:");
double subtrahend = scanner.nextDouble();
System.out.println("Ergebnis: " + (minuend - subtrahend));
break;
case 3:
System.out.println("Geben Sie den ersten Faktor ein:");
double factor1 = scanner.nextDouble();
System.out.println("Geben Sie den zweiten Faktor ein:");
double factor2 = scanner.nextDouble();
System.out.println("Ergebnis: " + (factor1 * factor2));
break;
case 4:
System.out.println("Geben Sie den Dividend ein:");
double dividend = scanner.nextDouble();
System.out.println("Geben Sie den Divisor ein:");
double divisor = scanner.nextDouble();
if (divisor != 0) {
System.out.println("Ergebnis: " + (dividend / divisor));
} else {
System.out.println("Ungültige Eingabe: Division durch Null.");
}
break;
case 5:
System.out.println("Geben Sie die Zahl für die Fakultät ein:");
int num = scanner.nextInt();
System.out.println("Ergebnis: " + fakultaet(num));
break;
case 6:
System.out.println("Geben Sie die Zahl ein:");
double base = scanner.nextDouble();
System.out.println("Ergebnis: " + (base * base));
break;
case 7:
System.out.println("Geben Sie die Zahl ein:");
double number = scanner.nextDouble();
if (number >= 0) {
System.out.println("Ergebnis: " + Math.sqrt(number));
} else {
System.out.println("Ungültige Eingabe: Negative Zahl.");
}
break;
case 8:
continueCalculations = false;
System.out.println("Programm beendet.");
break;
default:
System.out.println("Ungültige Auswahl. Bitte wählen Sie erneut.");
}
}
scanner.close();
}
public static int fakultaet(int n) {
if (n == 0)
return 1;
else
return (n * fakultaet(n - 1));
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: