Given a one-dimensional array that contains no more than 50 integers. Replace each element of the array with the sum of neighboring elements. If there are no neighboring elements, then the number is left unchanged. Here is the code:

import java.util.Scanner; class Laba5_1 { public static void main(String args[]) { Scanner input = new Scanner(System.in); int array[] = new int[10]; int aP[] = new int[10]; System.out.println("Введите элемент массива: "); for (int i = 0; i < 10; i++) { array[i] = input.nextInt(); } System.out.println("Введенный массив: "); for (int i = 0; i < 10; i++) { System.out.print(" " + array[i]); } System.out.println(); for (int i = 0; i < 10; i++) { if (i != 0 || i != 10) { int aP[j] = array[i - 1] + array[i + 1]; j++; } } System.out.println("Преобразованный массив: " + aP); } } 

Closed due to the fact that off-topic participants Kromster , aleksandr barakin , iluxa1810 , αλεχολυτ , HamSter 9 Nov '16 at 20:33 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, aleksandr barakin, iluxa1810, αλεχολυτ, HamSter
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What is the problem then? ) - Suvitruf
  • in i != 0 || i != 10 i != 0 || i != 10 , I think ... - zRrr
  • It looks like i <0 && i <9. Exclude extreme elements. And the condition i! = 0 || i! = 10 will always be true, regardless of i. - Aleksandr Balyunou

1 answer 1

I think you mean the following

  for (int i = 0; i < 10; i++) { if (i != 0 && i != 9) { ^^^^^^^^ int aP[i] = array[i - 1] + array[i + 1]; ^^^^^ } } 

In the loop, instead of the magic number 10, it is better to use the length property of the arrays. For example,

  for (int i = 0; i < aP.length; i++) {