Skip to content

Instantly share code, notes, and snippets.

@laim2003 laim2003/excercise.java
Last active Oct 29, 2018

Embed
What would you like to do?
package com.my.package;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
private static int[] nums = {1,4,5,7,8,9};//Some random SORTED array
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("array ="+ Arrays.toString(nums));
System.out.println("Give me a number:");
int num = sc.nextInt();
int posInArray = 0;
boolean existsInArray;
for(int i = 0;i < nums.length;i++){
existsInArray = num == nums[i];
posInArray = num > nums[i]?i+1:posInArray;
if(existsInArray){
posInArray = i;
System.out.println("Element exists in array");
break;
}
}
System.out.println("Position in array is/would be "+posInArray);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.