This repository was archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrakt1.java
More file actions
134 lines (127 loc) · 3.69 KB
/
Copy pathPrakt1.java
File metadata and controls
134 lines (127 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.company;
import java.util.Random;
import java.util.Scanner;
public class Main {
static void RandArray(int[] arr)
{
Random rnd = new Random(System.currentTimeMillis());
int min = -100;
int max = 100;
for (int i = 0; i < arr.length; i++) {
arr[i] = min + rnd.nextInt(max - min + 1);
}
}
static void OutputArr(int[] arr)
{
for (int a: arr) {
System.out.print(a +" ");
}
System.out.println("");
}
static int CountElement(int[] arr )
{
int count=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]<7)
{
count++;
}
}
return count;
}
static int SumElement(int[] arr)
{
int firstElementIndex=0, lastElementIndex=0;
int sum=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>0)
{
firstElementIndex=i;
break;
}
}
for(int i=firstElementIndex+1;i<arr.length;i++)
{
if(arr[i]>0)
{
lastElementIndex=i;
}
}
for(int i=firstElementIndex;i<=lastElementIndex;i++)
{
sum=sum+arr[i];
}
return sum;
}
static void Task1()
{
int size;
do {
System.out.println("Введіть розмір массива(>0): ");
size =new Scanner(System.in).nextInt();
}while(size<0);
int[] arr=new int[size];
RandArray(arr);
OutputArr(arr);
System.out.println("Кількість елементів < 7: "+ CountElement(arr));
System.out.println("Суму елементів масиву, розташованих між першим й останнім додатними елементами: "+SumElement(arr));
}
static void ChengElement(int [] arr) {
int maxElementIndex = 0, minElementIndex = 0;
for (int i = 1; i < arr.length; i++)
{
if(arr[i]>arr[maxElementIndex])
maxElementIndex=i;
}
for (int i = 1; i < arr.length; i++)
{
if(arr[i]<arr[minElementIndex])
minElementIndex=i;
}
int temp1;
temp1 = arr[minElementIndex];
arr[minElementIndex] = arr[maxElementIndex];
arr[maxElementIndex] = temp1;
}
static void Task2()
{
int size;
do {
System.out.println("Введіть розмір массива(>0): ");
size =new Scanner(System.in).nextInt();
}while(size<0);
int []arr=new int[size];
RandArray(arr);
OutputArr(arr);
ChengElement(arr);
OutputArr(arr);
}
public static void main(String[] args) {
Scanner num=new Scanner(System.in);
boolean run=true;
byte number;
while(run) {
System.out.println("Вибертіть номер завдання (1-2, 5-ВИХІД!): ");
number = num.nextByte();
switch (number) {
case 1:
System.out.println("Завдання 1: ");
Task1();
break;
case 2:
System.out.println("Завдання 2: ");
Task2();
break;
case 5:
System.out.println("Завершення виконання....");
run=false;
break;
default:
System.out.println("Не вірне значення:");
break;
}
}
}
}