天天看點

設在數組A中有10個整數 現從鍵盤中輸入一個數 檢查該數是否在數組總,若在數組中則輸出該書在數組總的位置若從數組中未找到該數 則輸出“NOT FOUND!”資訊

import java.util.Scanner;

public class Found{

   public static void main(String args[]){

System.out.println("請輸入10個整數");

Scanner sc=new Scanner(System.in);

int a[];a=new int[10];

    for(int i=0;i<10;i++)

       a[i]=sc.nextInt();

     System.out.println("請輸入一個要查找的數");

          Scanner m=new Scanner(System.in);

    int k=m.nextInt();int b=0;

   for(int i=0;i<10;i++){

    if(k==a[i])

    System.out.println("要查找的數是第"+( i+1)+"位");

  if(k!=a[i]) b++;

}

   if(b==10)

 System.out.print("NOT FOUND!");

}

}