從數組中查找數據

/*
*功能:從數組中查找數據
*/
class Shuzu {
    public static void main(String[] args)
    {
        int data [] = new int []{1,2,3,4,5,6};
            
        search(data,5); //從數組data中查找5
            
    }
    public static void search(int temp [],int y ){
            
        boolean flag = false;
        for(int x = 0;x < temp.length;x ++)
        {
            if(temp[x] == y){
                flag = true;
                break;}
                
        }
        if(flag){
            System.out.println("已經找到該數據");
            }else{
                System.out.println("沒有找到數據");
                }
        
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章