/* OperatoryLogiczne.java */
public class OperatoryLogiczne
{
public static void main(String[] args)
{
boolean a = false; // zmienne typu logicznego boolean
boolean b = true;
System.out.println("a = " + a);
System.out.println("b = " + b + "\n");
System.out.println("not a = " + !a);
System.out.println("a or b = " + (a || b));
System.out.println("a and b = " + (a && b));
System.out.println("a xor b = " + (a ^ b));
}
}
a = false b = true not a = true a or b = true a and b = false a xor b = true Press any key to continue...
/* DzielenieModulo.java */
// a % b - reszta z dzielenia a przez b
public class DzielenieModulo
{
public static void main(String[] args)
{
int a = 9;
int b = 2;
System.out.println(a + " % " + b + " = " + a%b);
}
}
9 % 2 = 1 Press any key to continue...
/* DzielenieBezReszty.java
int a = 5;
int b = 3;
a / b - dzielenie bez reszty, jesli a i b maja typ calkowity
*/
public class DzielenieBezReszty
{
public static void main(String[] args)
{
int a = 5;
int b = 3;
System.out.println(a + " / " + b + " = " + a/b);
}
}
5 / 3 = 1 Press any key to continue...
/* DzielenieReszta.java
int a = 5;
double b = 3;
a / b - dzielenie z reszta, jesli a lub b maja typ rzeczywisty
*/
public class DzielenieReszta
{
public static void main(String[] args)
{
int a = 5;
double b = 3;
System.out.println(a + " / " + b + " = " + a/b);
}
}
5 / 3.0 = 1.6666666666666667 Press any key to continue...
/* OdwracanieLiczb.java */
public class OdwracanieLiczb
{
public static void main(String[] args)
{
int n = 1234;
System.out.println(n);
while (n > 0)
{
int i = n % 10;
n = n / 10;
System.out.print(i);
}
System.out.println();
}
}
1234 4321 Press any key to continue...
Zadanie Dokonaj analizy programu OdwracanieLiczb.
n = 1234
"1234"
1234 > 0 i = 1234 % 10 = 4
n = 1234 / 10 = 123
'4'
123 > 0 i = 123 % 10 = 3
n = 123 / 10 = 12
'3'
12 > 0 i = 12 % 10 = 2
n = 12 / 10 = 1
'2'
1 > 0 i = 1 % 10 = 1
n = 1 / 10 = 0
'1'
0 > 0 false
""
/*
OperatorWarunkowy.java
Operator warunkowy ma postać:
wyrażenieLogiczne ? wyrażenie1 : wyrażenie2
Jeśli wyrażenie logiczne jest prawdziwe,
to operator zwraca wartość wyrażenie1.
Jeśli wyrażenie logiczne jest fałszywe,
to operator zwraca wartość wyrażenie2.
Oba zwracane wyrażenia muszą mieć ten sam typ.
*/
public class OperatorWarunkowy
{
public static void main(String[] args)
{
int a = -2; // należy zmieniać wartości a i b
int b = 0;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println(a < b ? "a < b" : "a >= b");
}
}
a = -2 b = 0 a < b Press any key to continue...
/* OperatoryPorownan.java */
public class OperatoryPorownan
{
public static void main(String[] args)
{
double a = 3.5; // double - typ zmiennopozycyjny
double b = -2;
System.out.println("a = " + a);
System.out.println("b = " + b);
if (a < b)
{
System.out.println("a mniejsze od b");
}
if (a <= b)
{
System.out.println("a mniejsze lub rowne b");
}
if (a == b)
{
System.out.println("a rowne b");
}
if (a >= b)
{
System.out.println("a wieksze lub rowne b");
}
if (a > b)
{
System.out.println("a wieksze od b");
}
if (a != b)
{
System.out.println("a rozne od b");
}
}
}
a = 3.5 b = -2.0 a wieksze lub rowne b a wieksze od b a rozne od b Press any key to continue...