2013-05-09 55 views
1

我用5種方法在Java中編寫了一個類。線性搜索,如果找到該值,則返回true;如果未找到,則返回false。線性搜索2,返回值的位置,如果找到。二進制搜索。它也搜索數組中的值,打印Int數組(每次打印10個數字)以及選擇排序,它對數組進行排序,以便我可以執行二分搜索。一切都很好,但由於某種原因,我的方法都沒有返回任何東西(void printIntArray方法除外)。方法在Java中沒有返回任何東西

編輯:


謝謝,夥計們,我沒有意識到我需要的。出於某種原因,我認爲它會自行返回價值......但另一個問題。 binarySearch方法似乎沒有做任何事情。在打印語句「使用二分搜索在隨機數組中搜索11」....之後,不會打印任何內容。


編輯2: 我的binarySearch方法是行不通的,因爲我不小心過中旬+ 1兩種else語句(否則,如果(鍵<陣列[MID])應該是中旬 - 1)。非常感謝大家!我添加了修復程序。

public class sortingSearching { 

    public static boolean linearSearch (int [] array, int key) { 
    for (int i = 0; i < array.length; i++) { 
     if (array [i] == key) 
      return true; 
    }// end for 
    return false; 
} 

public static int linearSearch2 (int [] array, int key) { 
    for (int i = 0; i < array.length; i++) { 
     if (array [i] == key) 
     return i; 
    }//end for 
    return -1; 
}//end linearSearch2 

public static boolean binarySearch (int [] array, int key) { 
    int left = 0; 
    int right = array.length - 1; 
    int mid = (left + right) /2; 
     while (left <= right) { 
      if (array[mid] == key) 
       return true; 
      else if (key < array[mid]) 
       right = mid - 1; 
      else 
       left = mid + 1; 
      mid = (left + right) /2; 
     } //end while 
    return false; 
}//end binarySearch 

public static void printIntArray (int [] array) { 
    for (int i = 0; i < array.length; i++) { 
     if (i%10 == 0) 
      System.out.println(); 
     System.out.print(array[i] + " "); 
    } // end for 
} 

public static void selectionSort (int [] array) { 
    for (int start = 0; start < array.length - 1; start ++) { 
     int minI = start; 
     for (int i = start + 1; i < array.length; i++) 
      if (array[i] < array[start]) 
       minI = i; 
     int temp = array[start]; 
    array[start] = array[minI]; 
     array[minI] = temp; 
    }//end for 
    } //end selectionSort 

public static void main (String args []) { 
    int [] array = new int [20]; 
    for (int i =0; i < array.length; i++) 
     array[i] = (int)((Math.random() * 100) + 1); 

    //print the array using printArray 
    printIntArray(array); 
    System.out.println(); 
    //use linearSearch to search for 30, 86, and 87 
    System.out.println("Searching for 30 in the random array. If true is returned, " + 
    "the value was found. If false was returned, the value was not found."); 
     System.out.println(linearSearch(array, 30)); 
    System.out.println("Searching for 86 in the random array. If true is returned, " + 
    "the value was found. If false was returned, the value was not found."); 
     System.out.println(linearSearch(array, 86)); 
    System.out.println("Searching for 87 in the random array. If true is returned, " + 
    "the value was found. If false was returned, the value was not found."); 
     System.out.println(linearSearch(array, 87)); 
    //use linearSearch to locate the first occurrences of 25, 80, and 91 
    System.out.println("Searching for the location of 25 in the random array. If -1 is " + 
    "returned, the number was not found in the array."); 
     System.out.println(linearSearch2(array, 25)); 
    System.out.println("Searching for the location of 80 in the random array. If -1 is " + 
    "returned, the number was not found in the array."); 
     System.out.println(linearSearch2(array, 80)); 
    System.out.println("Searching for the location of 91 in the random array. If -1 is " + 
    "returned, the number was not found in the array."); 
     System.out.println(linearSearch2(array, 91)); 
    //use selectionSort to sort the array 
    selectionSort(array); 
    //use binarySearch to search for 11, 28, 74, and 99 
    System.out.println("Searching for 11 in the random array using binary search. If true is returned, " + 
    "the value was found. If false was returned, the value was not found."); 
     System.out.println(binarySearch (array, 11)); 
    System.out.println("Searching for 28 in the random array using binary search. If true is returned, " + 
    "the value was found. If false was returned, the value was not found."); 
     System.out.println(binarySearch (array, 28)); 
    System.out.println("Searching for 74 in the random array using binary search. If true is returned, " + 
    "the value was found. If false was returned, the value was not found."); 
     System.out.println(binarySearch (array, 74)); 
    System.out.println("Searching for 99 in the random array using binary search. If true is returned, " + 
    "the value was found. If false was returned, the value was not found."); 
     System.out.println(binarySearch (array, 99)); 
} //end main 


} //end sortingSearching 

另外,我很抱歉main方法中的所有打印語句都讓人分心。我想把它們拿出來以方便閱讀,但我希望它能像我一直在運行它一樣。

+0

確保您的方法何時返回某物獲得回報。 IE:'VARIABLE = binarySearch(array,74)' – Tdorno 2013-05-09 19:57:57

+0

您是否調試過'binarySearch'函數? – christopher 2013-05-09 20:06:20

+0

請檢查我的編輯。 – christopher 2013-05-09 20:08:33

回答

3

它們不會返回任何內容,因爲您沒有將返回值分配給任何變量。

使此:

boolean foo= linearSearch(array, 86); 
System.out.println(foo); 

System.out.println(linearSearch(array, 86)); 

等。

+1

我認爲'linearSearch'返回一個布爾值:) – christopher 2013-05-09 20:01:00

4
linearSearch(array, 30); 

他們確實會返回一些東西。但做一些與返回值!

boolean value = linearSearch(array, 30); 
System.out.println(value); 

或更簡單:

System.out.println(linearSearch(array, 30)); 

在回答您的編輯

您需要啓動left1。你正在執行整數除法,它永遠不會達到零。因此right卡在1left總是比它少。

+2

+1對於確切時間 – 2013-05-09 19:59:37

+0

克里斯,這是完美的。感謝您回覆我的編輯。 – user2130057 2013-05-09 20:19:12

+0

那麼,如果這就是一切,一定要標記我的答案是正確的:) – christopher 2013-05-09 20:20:08

3

您必須將的排序/搜索方法調用println()語句,否則結果將不會被打印!就像這樣:

System.out.println(
    "Searching for 30 in the random array. If true is returned, " + 
    "the value was found. If false was returned, the value was not found." + 
    linearSearch(array, 30)); 

或者,將結果存儲在一個局部變量 - 但是,你必須將變量傳遞給println()

boolean result = linearSearch(array, 30); 
System.out.println(
    "Searching for 30 in the random array. If true is returned, " + 
    "the value was found. If false was returned, the value was not found." + 
    result); 
2

他們返回他們所應該,只是你選擇不對他們返回的東西做任何事情。

您可以通過在System.out.println()中包裝函數調用或使用ret = yourfunction(params)存儲返回值並稍後顯示ret來解決此問題。

相關問題