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

  • Well, actually, you drive one array three times in a cycle, you can shorten it and make comparisons as you type, the code will , but its number will noticeably decrease - Kostya M
  • and yes, you added two identical pieces of code - Kostya M
  • Do not save on spaces ... - Regent
  • If you are given an exhaustive answer, mark it as accepted. - Alexander Semprony Grakh

3 answers 3

There are several errors in the code from the question, so I’ll suggest a rewritten version, which is also shorter:

 public static void sequence() { Scanner scanner = new Scanner(System.in); System.out.println("Количество чисел"); int n = scanner.nextInt(); int[] array = new int[n]; for (int i = 0; i < n; i++) { System.out.println("Введите число: "); array[i] = scanner.nextInt(); } if (array[0] == array[1]) { System.out.println("Нет последовательности"); return; } boolean isAscending = array[0] < array[1]; for (int i = 2; i < array.length; i++) { if ((array[i - 1] >= array[i] && isAscending) || (array[i - 1] <= array[i] && !isAscending)) { System.out.println("Нет последовательности"); return; } } System.out.println(isAscending ? "Восходящая" : "Нисходящая"); } 
  • For the first pair of array elements, we define, we will check the array / sequence for ascending or descending
  • If some element does not satisfy the check, then we stop checking and deduce that there is no order in the sequence
  • If the array is successfully verified entirely, then we display the checked order.

PS According to the task it is not clear whether it is necessary to process the case n = 1 and what should be output in this case.


You can shorten the code of the algorithm somewhat to the detriment of clarity (plus the difference of neighboring numbers should not be less than Integer.MIN_VALUE and more than Integer.MAX_VALUE ):

 int signum = Integer.signum(array[1] - array[0]); for (int i = 1; i < array.length; i++) { if ((array[i] - array[i - 1]) * signum <= 0) { System.out.println("Нет последовательности"); return; } } System.out.println(signum == 1 ? "Восходящая" : "Нисходящая"); 
  • Disregarding the first number, you just need to fix array [0] by 1? - Shakhzod Yakubov
  • @ShakhzodYakubov why do not you want to take into account the first number? In the task it must be considered. "beyond the first one" in this context says that the first number should not be more / less than the previous one. And this is logical, since the first number simply has no previous one. - Regent
  • damn sure, thanks for responding now I will understand. I read about boolean but never used it - Shakhzod Yakubov
  • @ShakhzodYakubov, the only thing that I don’t like now in my decision is that it seems that in the task for the sequence 1 1 1 answer should be “No sequence”. I will correct the code. - Regent

The array numbering starts from the 0th element, and not from 1. Thus, if there are 10 elements in the array, then the numbering is 0..9, and in your case 0..10 everywhere. Therefore, the solution to the problem is to set the boundaries of the iteration correctly.

 for (int i = 2; i < array.length - 1; i++) 

Namely, it goes beyond the limits of the array you have in this line else if (array[i] > array[i + 1])

  • And about how to make the code easier, I probably agree with the fact that you can check immediately when entering numbers. That is, when adding numbers to an array in the same block, you can make a check on your sequence, and, let's say, add a flag ( boolean ascending ). And at the end of the input, if all entered checks are true, then the flag will be true, which means the ascending sequence. Once the check fails, the flag is false and there is no sequence. - Mishakov Alexander
  Scanner scanner = new Scanner(System.in); System.out.println("Количество чисел"); int[] array = new int[scanner.nextInt()]; for (int i = 0; i < array.length; i++) { System.out.println("Введите число: "); array[i] = scanner.nextInt(); } int [] val = IntStream.of(array).sorted().toArray(); if(Arrays.equals(array, val)) { System.out.println("Восходящая"); return; } for (int i = 0; i < val.length / 2; i++) { int tmp = val[i]; val[i] = val[val.length - i - 1]; val[val.length - i - 1] = tmp; } if(Arrays.equals(array, val)) System.out.println("Нисходящая"); else System.out.println("Нет последовательности."); 

From the beginning, the contents of the array entered. After a copy of array - val . First, the val sorted in ascending order, and then if they match, we display "Ascending". If not, val flipped and compared with array . If they match, it means that the array downward and the output is downward. If not, then "No sequence" is displayed.

The advantage of this method is that we do not resort to cycles (except for turning over the array, but this method is in all projects) and such code is easier to understand.

  • Everything would be fine, only I, as a newbie in Java (and in programming), do not know what IntStream is and further) But thanks for the answer, I will read what it is - Shakhzod Yakubov
  • @ShakhzodYakubov This line can be replaced by: int [] val = Arrays.copyOf(array, array.length; Arrays.sort(val) - Alexander Semprony Grakh