Advertisement
Guest User

IndexGuess

a guest
Apr 6th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. package com.company;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.  
  8.         int[] numberArray = {3, 5, 7, 12, 6};
  9.         int target = 12;
  10.  
  11.  
  12.         int indexNumber = GetIndex(numberArray, target);
  13.         System.out.println("Index found was: " + indexNumber);
  14.  
  15.     }
  16.  
  17.  
  18.  
  19.     public static int GetIndex(int[] nums, int target) {
  20.         for(int i=0; i<nums.length; i++) {
  21.             if(nums[i] == target) {
  22.                 return i;
  23.             }
  24.         }
  25.  
  26.  
  27.         return -1;
  28.     }
  29.  
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement