+ 1
Help!
Write a method that takes an array of integers and returns the position of the first zero (0) that it finds in the array.
5 Réponses
+ 2
public class Program
{
public static void main(String[] args)
{
int[] numbers = {5, 29, 12, 0, 66, 1, -30, 0, 45};
int idx = getIndexOfZero(numbers);
if (idx == - 1) {
System.out.println("(!) No '0' element is found...");
} else {
System.out.println("Index of first 0 is " + idx);
}
}
public static int getIndexOfZero(int[] numbers) {
for (int i = 0; i < numbers.length; i++)
{
if (numbers[i] == 0)
{
return i;
}
}
return -1;
}
}
Here it is. I used Tobias's example.
That way it should print a message, if there is no '0' at all.
+ 3
thanks😀
+ 3
thank you.i needed a return statement
+ 2
that's how my code looks. i need to add a method