Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kurs_java_alx_20240321
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
kurs_java_alx_20240321
Commits
59be7d4f
Commit
59be7d4f
authored
Mar 22, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Teoria if
parent
c79d41e3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
127 additions
and
0 deletions
+127
-0
Losowanie.java
src/p04_if/Losowanie.java
+39
-0
SpojnikiLogiczne.java
src/p04_if/SpojnikiLogiczne.java
+42
-0
TeoriaIf.java
src/p04_if/TeoriaIf.java
+46
-0
No files found.
src/p04_if/Losowanie.java
0 → 100644
View file @
59be7d4f
package
p04_if
;
import
java.util.Random
;
import
java.util.concurrent.ThreadLocalRandom
;
public
class
Losowanie
{
public
static
void
main
(
String
[]
args
)
{
// tworzymy obiekt Random - "generator liczb pseudolosowych"
Random
random
=
new
Random
();
double
d
=
random
.
nextDouble
();
// liczba z zakresu [0.0 , 1.0)
System
.
out
.
println
(
"double: "
+
d
);
// losowy int z całego zakresu intów (orientacyjnie od -2mld do +2mld)
int
x
=
random
.
nextInt
();
// int z zakresu od 0 do limit-1, czyli tu od 0 do 99
int
y
=
random
.
nextInt
(
100
);
// int z zakresu od 500 włącznie do 1000 wyłączając, czyli od 500 do 999
// dostępne od niedawna, od Java 17
int
z
=
random
.
nextInt
(
500
,
1000
);
System
.
out
.
println
(
x
+
" "
+
y
+
" "
+
z
);
// alternatywnie, bez tworzenia obiektu Random - każdy wątek ma swojego prywatnego randoma
// Od Java 7
int
r
=
ThreadLocalRandom
.
current
().
nextInt
();
System
.
out
.
println
(
r
);
char
losowaLitera1
=
(
char
)(
65
+
random
.
nextInt
(
26
));
char
losowaLitera2
=
(
char
)(
'A'
+
random
.
nextInt
(
'Z'
-
'A'
+
1
));
System
.
out
.
print
(
losowaLitera1
);
System
.
out
.
print
(
losowaLitera2
);
}
}
src/p04_if/SpojnikiLogiczne.java
0 → 100644
View file @
59be7d4f
package
p04_if
;
import
java.util.Random
;
public
class
SpojnikiLogiczne
{
public
static
void
main
(
String
[]
args
)
{
Random
random
=
new
Random
();
int
x
=
random
.
nextInt
(
100
);
int
y
=
random
.
nextInt
(
100
);
System
.
out
.
println
(
"Wylosowane liczby: "
+
x
+
", "
+
y
);
// Wprowadzenie: gdy sprawdzić dwa warunki i dopiero cś zrobić, to możemy
// a) zagniezdzić ifa - raczej "brzydkie", jedyną zaletą jest to, że mogę napisać oddzielne else do obu części warunku
if
(
x
>
50
)
{
if
(
y
>
50
)
{
System
.
out
.
println
(
"obie liczby >50"
);
}
else
{
System
.
out
.
println
(
"y nie jest >50"
);
}
}
else
{
System
.
out
.
println
(
"x nie jest >50"
);
}
System
.
out
.
println
();
// b) użyć spójników logicznych
// && - logiczne "i", "and", koniunkcja
if
(
x
>
50
&&
y
>
50
)
{
System
.
out
.
println
(
"obie liczby >50"
);
}
else
{
System
.
out
.
println
(
"jakaś liczba jest <=50"
);
}
// || - logiczne "lub", "or", alternatywa
if
(
x
>
50
||
y
>
50
)
{
System
.
out
.
println
(
"istnieje liczba >50"
);
}
else
{
System
.
out
.
println
(
"żadna z liczb nie jest >50"
);
}
}
}
src/p04_if/TeoriaIf.java
0 → 100644
View file @
59be7d4f
package
p04_if
;
import
java.util.Random
;
public
class
TeoriaIf
{
public
static
void
main
(
String
[]
args
)
{
Random
random
=
new
Random
();
int
x
=
random
.
nextInt
(
100
);
int
y
=
random
.
nextInt
(
100
);
System
.
out
.
println
(
"Wylosowane liczby: "
+
x
+
", "
+
y
);
// najbardziej typy zapis: if z else.
// jeśli warunek logiczny prawdziwy, to wykouje się fragment kodu za if-em
// a jeśli fałszywy, to wykonuje się fragment kodu za else.
if
(
x
+
y
>=
100
)
{
System
.
out
.
println
(
"suma liczb >= 100"
);
}
else
{
System
.
out
.
println
(
"suma liczb < 100"
);
}
// Jeśli do wykonania jest tylko jedna instrukcja, można jej nie brać w {klamerki}
// (ale ja je daję nawet w takich przypadkach)
if
(
x
%
2
==
0
)
System
.
out
.
println
(
"x jest parzysty"
);
else
System
.
out
.
println
(
"x jest nieparzysty"
);
// można pisać if bez towarzyszącego else
if
(
x
<
10
)
{
System
.
out
.
println
(
"x jest jednocyfrowy"
);
}
System
.
out
.
println
(
"To się zawsze wypisze"
);
// można też pisać ciąg wielu ifów na zasadzie if - else if - else
if
(
x
>
y
)
{
System
.
out
.
println
(
"x jest większy"
);
}
else
if
(
x
<
y
)
{
System
.
out
.
println
(
"y jest większy"
);
}
else
{
System
.
out
.
println
(
"obie liczby równe"
);
}
}
}
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