-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx8.java
More file actions
31 lines (26 loc) · 907 Bytes
/
Ex8.java
File metadata and controls
31 lines (26 loc) · 907 Bytes
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
package java_exercises_1_sda;
//Sprawdź, czy podany ciąg liczb jest ciągiem arytmetycznym
public class Ex8 {
public static void main(String[] args) {
int[]arrayIsNotCorrect = {4, 52, 4, 7};
int[]arrayIsCorrect = {1, 5, 9, 13};
Ex6.tablePrint(arrayIsCorrect);
boolean result = isArithmetic(arrayIsCorrect);
System.out.println(result);
}
public static boolean isArithmetic(int[] arrayArithmetic) {
if (arrayArithmetic.length < 2) {
return false;
}
boolean isCorrect = true;
int gap = arrayArithmetic[1] - arrayArithmetic[0];
for (int i = 2; i < arrayArithmetic.length; i++){
int actualGap = arrayArithmetic[i] - arrayArithmetic[i-1];
if (actualGap != gap){
isCorrect = false;
break;
}
}
return isCorrect;
}
}