Условный оператор. Циклы. (Java)
Перейти к навигации
Перейти к поиску
Условный оператор
- Условный оператор (справка Oracle)
- Операторы сравнения (справка Oracle)
- Оператор выбора switch (справка Oracle)
1 package ru.example;
2
3 import java.util.Scanner;
4
5 public class Main {
6
7 public static void main(String[] args) {
8 Scanner scanner = new Scanner(System.in);
9
10 System.out.println("Please enter water temperature in degrees celsius:");
11 int temp = scanner.nextInt();
12
13 if (temp >= 100)
14 {
15 System.out.println("Water is boiling.");
16 }
17 else
18 {
19 System.out.println("Not hot enough.");
20 }
21
22 System.out.println((temp % 2 == 0) ? "Temperature is even" : "Temperature is odd");
23
24
25 System.out.println("Please enter ticket class:");
26 int ticketClass = scanner.nextInt();
27 switch (ticketClass)
28 {
29 case 1:
30 System.out.println("This is first class.");
31 break;
32 case 2:
33 System.out.println("This is second class.");
34 break;
35 case 3:
36 System.out.println("This is third class.");
37 break;
38 default:
39 System.out.println("This is unknown class.");
40 break;
41 }
42 }
43 }
Циклы
1 package ru.example;
2
3 import java.util.Scanner;
4
5 public class Main {
6
7 public static void main(String[] args) {
8
9 System.out.println("while loop");
10 int i = 1;
11 while (i < 11)
12 {
13 System.out.println(i);
14 i++;
15 }
16
17 System.out.println("do while loop");
18 i = 1;
19 do
20 {
21 System.out.println(i);
22 i++;
23 } while (i < 11);
24
25 System.out.println("for loop");
26 for (int j = 1; j < 11; j++)
27 {
28 System.out.println(j);
29 }
30 }
31 }