+ 2

Hello Coders.... What am I missing here.. The code ain't running.

2nd Mar 2017, 5:59 PM
Ricardo Chitagu
Ricardo Chitagu - avatar
3 ответов
+ 11
I don't understand what your code should do, but at least this one compiles: public class Program{ static int total =0; public static void main(String[] args) { int[]arr={2,4,6}; int result = getTotal (arr); for (int i=0;i<arr.length;i++){ total = (total += arr[i]); if (arr [i]%2==0) { if (total ==12){ System.out.println ("1") ; } else System.out.println ("0"); } } } public static int getTotal(int[] a) { int result=0; for(int i=0; i<a.length; i++){ total += a[i]; if (a[i]%2==0) { if (total ==12){ result =1 ; } else result =0; } } return result; } } - Some brackets were missing or at the wrong place. - your result wasn't always returned, because it was nested in a if statement and the loop. - int result in the method wasn't initiated - int total wasn't declared in the method - I didn't find a nested loop...
2nd Mar 2017, 6:16 PM
Tashi N
Tashi N - avatar
+ 3
public class Program { public static void main(String[] args) { int[]arr={2,4,6}; int total =0; int result = getTotal (arr); for (int i=0;i<arr.length;i++){ total = (total += arr[i]); if (arr [i]%2==0) { if (total ==12){ System.out.println ("1") ; } else System.out.println ("0"); } } } } public static int getTotal(int [] a){ int result; for (int i=0;i<a.length;i++){ total = (total += a[i]); if (a[i]%2==0) { if (total ==12){ result =1 ; } else result =0; return result ; } } } //*nestedLoop THIS IS THE CODE!!!
2nd Mar 2017, 6:00 PM
Ricardo Chitagu
Ricardo Chitagu - avatar
+ 2
First off, Your method is out of class. Look at your braces carefully. Next your variable total is not public.
2nd Mar 2017, 6:17 PM
Meharban Singh
Meharban Singh - avatar