Commit 3fa7f49a by Patryk Czarnik

kalkulator - switch

parent 1ad89f46
...@@ -32,17 +32,25 @@ public class Kalkulator extends HttpServlet { ...@@ -32,17 +32,25 @@ public class Kalkulator extends HttpServlet {
<form> <form>
Podaj pierwszą liczbę: <input type='text' name='liczba1'><br> Podaj pierwszą liczbę: <input type='text' name='liczba1'><br>
Podaj drugą liczbę: <input type='text' name='liczba2'><br> Podaj drugą liczbę: <input type='text' name='liczba2'><br>
Podaj znak działania: <input type='text' name='operacja'><br>
<button>Oblicz</button> <button>Oblicz</button>
</form> </form>
"""); """);
String operacja = request.getParameter("operacja");
String liczba1 = request.getParameter("liczba1"); String liczba1 = request.getParameter("liczba1");
String liczba2 = request.getParameter("liczba2"); String liczba2 = request.getParameter("liczba2");
if(liczba1 != null && liczba2 != null) { if(operacja != null && liczba1 != null && liczba2 != null) {
long x = Long.parseLong(liczba1); long x = Long.parseLong(liczba1);
long y = Long.parseLong(liczba2); long y = Long.parseLong(liczba2);
long wynik = x + y; long wynik = switch(operacja) {
case "+" -> x + y;
case "-" -> x - y;
case "*" -> x * y;
case "/" -> x / y;
case "%" -> x % y;
default -> 0;
};
out.append("<div>Wynik: " + wynik + "</div>"); out.append("<div>Wynik: " + wynik + "</div>");
} }
......
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