Commit 3726039a by Patryk Czarnik

kalk 1-metodowy 2

parent 35ec2d32
package kalkulatory;
public class KalkulatorJednometodowy {
public static int oblicz(int liczba1, int liczba2, String operacja) {
return switch (operacja) {
case "+" -> liczba1 + liczba2;
case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2;
case "/" -> liczba1 / liczba2;
default -> throw new IllegalArgumentException("Nieznane " + operacja);
};
}
}
...@@ -13,19 +13,10 @@ public class ProgramJednometodowy { ...@@ -13,19 +13,10 @@ public class ProgramJednometodowy {
} }
int liczba1 = sc.nextInt(); int liczba1 = sc.nextInt();
int liczba2 = sc.nextInt(); int liczba2 = sc.nextInt();
int wynik = oblicz(liczba1, liczba2, operacja); int wynik = KalkulatorJednometodowy.oblicz(liczba1, liczba2, operacja);
System.out.printf("%d %s %d = %d\n", liczba1, operacja, liczba2, wynik); System.out.printf("%d %s %d = %d\n", liczba1, operacja, liczba2, wynik);
} }
System.out.println("nara"); System.out.println("nara");
} }
public static int oblicz(int liczba1, int liczba2, String operacja) {
switch(operacja) {
case "+": return liczba1 + liczba2;
case "-": return liczba1 - liczba2;
case "*": return liczba1 * liczba2;
case "/": return liczba1 / liczba2;
default: throw new IllegalArgumentException("Nieznane " + operacja);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment