Telor_Goreng0

nomor 1 pertemuan 3

Apr 10th, 2021
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. package pertemuan3;
  2. import java.util.Scanner;
  3. public class nomor1{
  4. public static int index(int data[], int key){
  5. for(int i=0;i<data.length;i++){
  6. if(data[i] == key){
  7. return i;
  8. }
  9. }
  10. return -1;
  11. }
  12. public static void main(String[]args){
  13. int[] data= {12, 90, 77, 13, 36, 39, 9, 89, 99, 1, 30, 27};
  14. Scanner in = new Scanner(System.in);
  15. System.out.print("Data : ");
  16. for (int i = 0; i < data.length; i++){
  17. System.out.print(data[i]+" ");
  18. }
  19. System.out.print("\nMasukkan angka berapa yang ingin kamu cari : ");
  20. int key = in.nextInt();
  21. int result = index(data, key);
  22. if (result == -1)
  23. System.out.println("Angka tidak ditemukan");
  24. else
  25. System.out.println("Angka "+key+" ditemukan di index ke-"+index(data, key));
  26. }
  27. }
Add Comment
Please, Sign In to add comment