Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
javab_20230617
Commits
6d75f364
Commit
6d75f364
authored
Jul 01, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Kalkulator - wiele działań
parent
2acb7042
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
9 deletions
+26
-9
Kalkulator.java
...-Servlety/src/main/java/serwlety/podstawy/Kalkulator.java
+26
-9
No files found.
PC23-Servlety/src/main/java/serwlety/podstawy/Kalkulator.java
View file @
6d75f364
...
@@ -23,14 +23,19 @@ public class Kalkulator extends HttpServlet {
...
@@ -23,14 +23,19 @@ public class Kalkulator extends HttpServlet {
<body>"""
;
<body>"""
;
final
String
formularz
=
"""
final
String
formularz
=
"""
<form>
<form>
<label for="
liczba1
">Pierwsza liczba:</label>
<input name="
liczba1
" type="
number
">
<input name="
liczba1
">
<select name='operacja'>
<option value='+'>+</option>
<option value='-'>-</option>
<option value='*'>*</option>
<option value='/'>/</option>
</select>
<input name="
liczba2
" type="
number
">
<br/>
<br/>
<label for="
liczba2
">Druga liczba:</label>
<button>Oblicz</button>
<input name="
liczba2
">
<br/>
<button>Wyślij</button>
</form>"""
;
</form>"""
;
final
String
koniec
=
"""
final
String
koniec
=
"""
</body>
</body>
</html>"""
;
</html>"""
;
...
@@ -41,13 +46,25 @@ public class Kalkulator extends HttpServlet {
...
@@ -41,13 +46,25 @@ public class Kalkulator extends HttpServlet {
out
.
println
(
formularz
);
out
.
println
(
formularz
);
try
{
try
{
int
liczba1
=
Integer
.
parseInt
(
request
.
getParameter
(
"liczba1"
));
long
liczba1
=
Long
.
parseLong
(
request
.
getParameter
(
"liczba1"
));
int
liczba2
=
Integer
.
parseInt
(
request
.
getParameter
(
"liczba2"
));
long
liczba2
=
Long
.
parseLong
(
request
.
getParameter
(
"liczba2"
));
out
.
println
(
"<div>Suma "
+
(
liczba1
+
liczba2
)
+
"</div>"
);
String
operacja
=
(
request
.
getParameter
(
"operacja"
));
long
wynik
=
oblicz
(
liczba1
,
liczba2
,
operacja
);
out
.
println
(
"<div>Wynik "
+
wynik
+
"</div>"
);
}
catch
(
NumberFormatException
e
)
{
}
catch
(
NumberFormatException
e
)
{
out
.
println
(
"Niepoprawny format liczby"
);
out
.
println
(
"Niepoprawny format liczby"
);
}
}
out
.
println
(
koniec
);
out
.
println
(
koniec
);
}
}
private
long
oblicz
(
long
liczba1
,
long
liczba2
,
String
operacja
)
{
return
switch
(
operacja
)
{
case
"+"
->
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
default
->
0
;
};
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment