lab description : write a program that will search a list to find the first odd number. if an odd number is found, then find the first even number following the odd number. return the distance between the first odd number and the last even number. return -1 if no odd numbers are found or there are no even numbers following an odd number.

Respuesta :

Using the knowledge in computational language in JAVA it is possible to write a code that  will search a list to find the first odd number.

Writting the code:

public class RayOddToEven {

   public static int go(int[] ray) {

       int oddIndex = -1;

       for (int i = 0; i < ray.length; i++) {

           if (ray[i] % 2 == 1) {

               oddIndex = i;

               break;

           }

       }

       int evenIndex = -1;

       for (int i = oddIndex+1; i < ray.length; i++) {

           if (ray[i] % 2 == 0) {

               evenIndex = i;

               break;

           }

       }

       if (oddIndex >= 0 && evenIndex >= 0) {

           return evenIndex - oddIndex;

       } else {

           return -1;

       }

   }

}

public class OddToEvenRunner {

   public static void main(String[] args) {

       System.out.println(RayOddToEven.go(new int[] {7, 1, 5, 3, 11, 5, 6, 7, 8, 9, 10, 12345, 11}));

       System.out.println(RayOddToEven.go(new int[] {11, 9, 8, 7, 6, 5, 4, 3, 2, 1, -99, 7}));

       System.out.println(RayOddToEven.go(new int[] {10, 20, 30, 40, 5, 41, 31, 20, 11, 7}));

       System.out.println(RayOddToEven.go(new int[] {32767, 70, 4, 5, 6, 7}));

       System.out.println(RayOddToEven.go(new int[] {2, 7, 11, 21, 5, 7}));

       System.out.println(RayOddToEven.go(new int[] {7, 255, 11, 255, 100, 3, 2}));

       System.out.println(RayOddToEven.go(new int[] {9, 11, 11, 11, 7, 1000, 3}));

       System.out.println(RayOddToEven.go(new int[] {7, 7, 7, 11, 2, 7, 7, 11, 11, 2}));

       System.out.println(RayOddToEven.go(new int[] {2, 4, 6, 8, 8}));

   }

}

See more about JAVA at brainly.com/question/18502436

#SPJ1

Ver imagen lhmarianateixeira
ACCESS MORE
EDU ACCESS