`package com.example.laba12; import java.util.Scanner; public class Laba12 { public static void main(String[] args) { int Sum = 0; Scanner s = new Scanner(System.in); int[]a= new int[10]; int i=0; System.out.println("Введите элементы массива"); while ( i < 10) { a[i]=s.nextInt(); Sum=Sum+a[i]; i++; } System.out.println("Сумма ="+Sum); } } 

`Could you tell me what my mistake is? enter image description here

Closed due to the fact that the participants are off topic : , Lex Hobbit , Cheg , cheops , αλεχολυτ Sep 16 '17 at 6:49 .

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 . " - Yuriy SPb, Lex Hobbit, Cheg, cheops, αλεχολυτ
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    in that 0 instead of the sum - teran
  • 2
    Add code as text instead of image. - 0xdb
  • while (i <10) {a [i] = s.NextInt (); Sum + = a [i]; i ++; } - SWR

1 answer 1

First, the error is that the code is shown in the screenshot. Second, you increment i before adding a[i] to the sum. Thus, in the expression sum=sum+a[i] a[i] always 0, except for the last iteration — it goes beyond the array boundaries. If you were using a debugger, you would not have to ask a question here.

  • Thanks for the help, I edited the code and wrote it not in the question itself. - Grigory