+ 1

Guys I need this to run what am I missing?

public class Program { public static void main(String[] args) { int[]arr={2,4,6}; int total =0; int even = 1; int odd=0; for (int i=0;i<arr.length;i++){ total = (total += arr[i]); if (arr[i]%2==0 && total=12) { return even ; break ; } } return odd; } }

28th Feb 2017, 2:12 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
9 Réponses
+ 2
thx sieben that shud wrk
28th Feb 2017, 2:47 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
+ 1
OK sieben... so how should I type it?
28th Feb 2017, 2:20 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
+ 1
the task is to return 1 if the array has a number divisible by two and has a sum of 12 else it should return 0.
28th Feb 2017, 2:31 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
+ 1
sieben the code is not outputting 1
28th Feb 2017, 7:17 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
+ 1
it's OK fixed it
28th Feb 2017, 7:33 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
+ 1
public class Program { public static void main(String[] args) { int[]arr={2,4,6}; int total =0; 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"); } } } } //*nestedLoop here is wat I did
28th Feb 2017, 7:43 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
A void function does not return anything. The break after the return is dead code. The String array you pass to your main is not beeing used.
28th Feb 2017, 2:16 AM
siebenWinkel
siebenWinkel - avatar
0
Well what is the task? You might use System.out.println(even); instead of return for printing to your console. And add an = to the = in the if statement ;)
28th Feb 2017, 2:27 AM
siebenWinkel
siebenWinkel - avatar
0
if by num you mean number of elements it might look like this.. public static void main(String[] args) { int[]arr={2,4,6}; int total =0; for (int i=0;i<arr.length;i++){ total = (total += arr[i]); } if(total==12 && arr.length%2==0){ System.out.println("1"); }else{ System.out.println("0"); } }
28th Feb 2017, 2:44 AM
siebenWinkel
siebenWinkel - avatar