Lesson4:Decision structures(if; if, else; if, else if, else; switch case), Repetition(while, do while, for)

1.  

Scanner in = new Scanner(System.in);
int score = in.nextInt();
char grade;
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else if (score >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println(grade);

Problem with construction of wrong logic, be cautious!

Scanner in = new Scanner(System.in);
int score = in.nextInt();
if (score >= 30) {
System.out.println(30);
} else if (score >= 40) {
System.out.println(40);
} else if (score >= 50) {
System.out.println(50);
} else if (score >= 60) {
System.out.println(60);
} else {
System.out.println("High");
}

2. == evezine sehven = qoymayin

boolean b = false;
if (b = true) {
System.out.println("case1");
} else {
System.out.println("case2");
}
boolean b;
int i = (int) (Math.random() * 1000);
int j = (int) (Math.random() * 1000);
if (b = i < j ? true : false) {
System.out.println(b + " " + i + " < " + j);
} else {
System.out.println(b + " " + j + " < " + i);
}

3. Statement problem

int i = 2;
if (i < 3)
int ii = 100;

solution:

int ii;
int i = 2;
if (i < 1)
ii = 100;
System.out.println(ii);


TASK 1: 

0 - 120 araliginda olan insanlarin yawini tapmaq:

Wert: < 0 ve > 120 stop

0 - 3 korpe; 4 - 12 uwaq; 12 - 19 yeniyetme; 20 - 30 genc; 31 - 59 orta yawli; 60 - 120 yawli

TASK 2:

Random sayda aile uzvlerinin sayini generasiya edirsiz.

Random sayda her bir aile uzvune yaw generasiya edirsiz

En sonda her yaw kateqoriyasindan nece nefer var ve yawlari necedir deye cap edirsiz.


1. While

int i = new Random().nextInt(101);
while (i < 51) {
System.out.println(i);
i = new Random().nextInt(101);
}
System.out.println("Done");
int i = (int) (10 * Math.random());
int j = (int) (10 * Math.random());
boolean b = i > j;

while (b = !b)
System.out.println("i: " + i + " j: " + j);

System.out.println("Second while");
while (b = i > j ? true : false) {
System.out.println("i: " + i + " j: " + j);
i = (int) (10 * Math.random());
j = (int) (10 * Math.random());
}

2. Do while

double r = Math.random();
System.out.println("r: " + r);
do {
System.out.println(r);
r = Math.random();
} while (r < 0.8d);

3. For

int sum = 0;
for(int i = 1; i <= 100; i++){
sum += 1;
}
System.out.println("sum: " + sum);

Vurma cedveli:

for(int i = 1; i <= 10; i++) {
for(int j = 1; j <= 10; j++) {
System.out.format("%4d", i * j);
}
System.out.println();
}

Fibonacci Numbers:

int first = 0;
int second = 1;
int third = 0;
for (int i = 0; i < 10; i++) {
System.out.format("%4d", first);
third = first + second;
first = second;
second = third;
}

A bit complicated for:

for (int i = 1, j = i + 10; (i < 5 & j > 2); i++, j = i * 2) {
System.out.println("i = " + i + " j = " + j);
}
for(char c = 0; c < 65535; c++) {
System.out.println("value: " + (int) c + " character: " + c);
}

Exercising over shapes: 

1. Dordbucaq

for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
System.out.print("*");
}
System.out.println();
}

2. Ici bow dordbucaq

for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (i == 0 || i == 9)
System.out.print("*");
else {
if (j == 0 || j == 9)
System.out.print("*");
else
System.out.print(" ");
}
}
System.out.println();
}



Switch Case

Scanner in = new Scanner(System.in);
int i = in.nextInt();

switch (i) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
default:
System.out.println("Error");
break;
}

Switch byte, Byte, short, Short,  int, Integer,  char, Character ve String qebul ede biler.


*Break and Continue

for (int i = 0; i < 10; i++) {
System.out.println(i);
if (i == 7)
break;
}
for (int i = 0; i < 10; i++) {
if (i == 7) {
continue;
}
System.out.println(i);
}
for (int a = 0; a < 5; a++) {
for (int b = 0; b < 5; b++) {
if(b == 3)
break;
System.out.println("a = " + a + " || b = " + b);
}
System.out.println("------------------------");
}

Label anlayiwi:

outer:
for (int a = 0; a < 5; a++) {
inner:
for (int b = 0; b < 5; b++) {
if(b == 3)
break outer;
System.out.println("a = " + a + " || b = " + b);
}
System.out.println("------------------------");
}







Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class