Java

Материал из Информационная безопасностя
Перейти к навигации Перейти к поиску

Hello World

Объявление переменных

1 int a = 5;
2 double b = 5.0 / 2;
3 boolean test = true;
4 char firstLetter = 'C';
5 String name = "Hi, there!";
6 int[] age = new int[5];
7 int[] age2 = {3, 7, 15, 21, 7};

Ввод и вывод

1 Scanner in = new Scanner(System.in);
2 
3 System.out.println("Please, enter your age:");
4 int age = in.nextInt();
5 System.out.println("Your age = " + age);

Условный оператор

 1 Scanner in = new Scanner(System.in);
 2 
 3 System.out.println("Please, enter your age:");
 4 int age = in.nextInt();
 5 System.out.println("Your age = " + age);
 6 if (age > 65)
 7 {
 8 	System.out.println("Here is your pension.");
 9 }
10 else
11 {
12 	System.out.println("Good morning people!");
13 }

Циклы

 1 System.out.println("while loop");
 2 int i = 1;
 3 while (i < 11)
 4 {
 5     System.out.println(i);
 6     i++;
 7 }
 8 
 9 System.out.println("do while loop");
10 i = 1;
11 do
12 {
13     System.out.println(i);
14     i++;
15 } while (i < 11);
16 
17 System.out.println("for loop");
18 for (int j = 1; j < 11; j++)
19 {
20     System.out.println(j);
21 }