/* * Unit 6 - Lesson 2 - Algorithms - Searching */ import java.io.*; import static java.lang.System.*; import java.util.Scanner; import java.lang.Math; class U6_L2_template{ public static void main (String str[]) throws IOException { Scanner scan = new Scanner (System.in); double list [] = {2.3 , 4.7 , 5.25 , 9.5 , 2.0 , 1.2 , 7.129 , 5.4 , 9.5 }; System.out.println( "What are you looking for? "); double look = scan.nextDouble(); //search for value in the array, print -1 if not found int where = -1; for (int i = 0; i < list.length; i++) { if (list[i] == look){ where = i; break; } } if (where == -1){ System.out.println("Not Found"); } else { System.out.println("Value at: " + where); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter