The task itself:
reads a positive integer number n
reads n integer values and:
o displays the message “ascending sequence” if any number
o displays the message “descending sequence” if it’s not
o displays the message “neither ascending nor descending sequence”
Example: assume n = 10 and the following 10 numbers introduced: -2 5 7 13 18 24 40 56 90 137. Then as the program is “ascending sequence”.
- Knowing only the basics of Java, is it possible to write better than my writings? Better - less, more logical
- I'm going beyond the array. How to fix?
The code that I got is:
public static void sequence() { Scanner scanner = new Scanner(System.in); System.out.println("Количество цифр"); int n = scanner.nextInt(); int[] array = new int[n]; int a; for (int i = 0; i < n ; i++) { System.out.println("Введите число: "); array[i] = a = scanner.nextInt(); } if (array[1] > array[2]) { for (int i = 2; i < array.length; i++) { if (i == array[n - 1]) { if (array[n - 2] > array[n - 1]) { System.out.println("Восходящая"); } } else if (array[i] > array[i + 1]) { continue; } System.out.println("Нет последовательности"); } } if (array[1] < array[2]) { for (int i = 2; i < array.length; i++) { if (i == array[n - 1]) { if (array[n - 2] > array[n - 1]) { System.out.println("Восходящая"); } } else if (array[i] < array[i + 1]) { continue; } System.out.println("Нет последовательности"); } } Thank you all, corrected the code, I do not go beyond the limits, but it does not give the result of the whole comparison. I think further