Skip to content

Instantly share code, notes, and snippets.

@mariiaKolokolova
Created May 6, 2020 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariiaKolokolova/a902694be0c8356ae30ab0d31d3a68c2 to your computer and use it in GitHub Desktop.
Save mariiaKolokolova/a902694be0c8356ae30ab0d31d3a68c2 to your computer and use it in GitHub Desktop.
package maricka.kolokolova;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Input length of array:");
int n = sc.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("Input [" + i + "] element:");
array[i] = sc.nextInt();
}
System.out.println("What do you want to find?");
int find = sc.nextInt();
System.out.println("Index for " + find + " is " + findIndex(array, find));
sc.close();
}
static int findIndex(int[] array, int find) {
int ind = -1;
for (int i = 0; i < array.length; i++) {
if (array[i] == find) {
ind = i;
break;
}
}
return ind;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment