+ 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; } }
9 Answers
+ 2
thx sieben that shud wrk
+ 1
OK sieben... so how should I type it?
+ 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.
+ 1
sieben the code is not outputting 1
+ 1
it's OK fixed it
+ 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
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.
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 ;)
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");
}
}