Java: различия между версиями

Материал из Информационная безопасностя
Перейти к навигации Перейти к поиску
 
Строка 5: Строка 5:
  
 
== Объявление переменных ==
 
== Объявление переменных ==
<syntaxhighlight lang="python" line>
+
<syntaxhighlight lang="java" line>
 +
int a = 5;
 +
double b = 5.0 / 2;
 +
boolean test = true;
 +
char firstLetter = 'C';
 +
String name = "Hi, there!";
 +
int[] age = new int[5];
 +
int[] age2 = {3, 7, 15, 21, 7};
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== Ввод и вывод ==
 
== Ввод и вывод ==
<syntaxhighlight lang="python" line>
+
<syntaxhighlight lang="java" line>
 +
Scanner in = new Scanner(System.in);
 +
 
 +
System.out.println("Please, enter your age:");
 +
int age = in.nextInt();
 +
System.out.println("Your age = " + age);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== Условный оператор ==
 
== Условный оператор ==
<syntaxhighlight lang="python" line>
+
<syntaxhighlight lang="java" line>
 +
Scanner in = new Scanner(System.in);
 +
 
 +
System.out.println("Please, enter your age:");
 +
int age = in.nextInt();
 +
System.out.println("Your age = " + age);
 +
if (age > 65)
 +
{
 +
System.out.println("Here is your pension.");
 +
}
 +
else
 +
{
 +
System.out.println("Good morning people!");
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== Циклы ==
 
== Циклы ==
<syntaxhighlight lang="python" line>
+
<syntaxhighlight lang="java" line>
 +
System.out.println("while loop");
 +
int i = 1;
 +
while (i < 11)
 +
{
 +
    System.out.println(i);
 +
    i++;
 +
}
 +
 
 +
System.out.println("do while loop");
 +
i = 1;
 +
do
 +
{
 +
    System.out.println(i);
 +
    i++;
 +
} while (i < 11);
 +
 
 +
System.out.println("for loop");
 +
for (int j = 1; j < 11; j++)
 +
{
 +
    System.out.println(j);
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Текущая версия на 15:02, 27 ноября 2020

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 }