Commit 39a77ced by Patryk Czarnik

kalkulator - select/option

parent 3fa7f49a
...@@ -30,9 +30,15 @@ public class Kalkulator extends HttpServlet { ...@@ -30,9 +30,15 @@ public class Kalkulator extends HttpServlet {
out.append(""" out.append("""
<form> <form>
Podaj pierwszą liczbę: <input type='text' name='liczba1'><br> Podaj pierwszą liczbę: <input type='number' name='liczba1'><br>
Podaj drugą liczbę: <input type='text' name='liczba2'><br> Podaj drugą liczbę: <input type='number' name='liczba2'><br>
Podaj znak działania: <input type='text' name='operacja'><br> Podaj znak działania:
<select name='operacja'>
<option value='+'>+</option>
<option value='-'>+</option>
<option value='*'>×</option>
<option value='/'>÷</option>
</select><br>
<button>Oblicz</button> <button>Oblicz</button>
</form> </form>
"""); """);
...@@ -41,17 +47,21 @@ public class Kalkulator extends HttpServlet { ...@@ -41,17 +47,21 @@ public class Kalkulator extends HttpServlet {
String liczba1 = request.getParameter("liczba1"); String liczba1 = request.getParameter("liczba1");
String liczba2 = request.getParameter("liczba2"); String liczba2 = request.getParameter("liczba2");
if(operacja != null && liczba1 != null && liczba2 != null) { if(operacja != null && liczba1 != null && liczba2 != null) {
long x = Long.parseLong(liczba1); try {
long y = Long.parseLong(liczba2); long x = Long.parseLong(liczba1);
long wynik = switch(operacja) { long y = Long.parseLong(liczba2);
case "+" -> x + y; long wynik = switch(operacja) {
case "-" -> x - y; case "+" -> x + y;
case "*" -> x * y; case "-" -> x - y;
case "/" -> x / y; case "*" -> x * y;
case "%" -> x % y; case "/" -> x / y;
default -> 0; case "%" -> x % y;
}; default -> 0;
out.append("<div>Wynik: " + wynik + "</div>"); };
out.append("<div>Wynik: " + wynik + "</div>");
} catch (Exception e) {
out.append("<div>Błąd: " + e + "</div>");
}
} }
out.append(""" out.append("""
......
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