java - 方法在Java中什么都不返回

原文 标签 java sorting search methods

我用5种方法用Java写了一个类。线性搜索,如果找到该值,则返回true;如果找不到该值,则返回false。线性搜索2,如果找到,则返回值的位置。二进制搜索。它也在数组中搜索值,print Int数组,一次打印10个数字,selection sort,对数组进行排序,这样我就可以进行二进制搜索。一切都可以正常编译,但是由于某种原因,我的任何方法都未返回任何内容(void printIntArray方法除外)。

编辑:



谢谢,伙计们,我没意识到我需要那个。由于某种原因,我认为它会自行返回值。 binarySearch方法似乎没有做任何事情。在打印语句“使用二进制搜索在随机数组中搜索11”之后,没有任何输出。



编辑2:
我的binarySearch方法无法正常工作,因为我不小心为其他两个语句都设置了mid + 1(否则,如果(key

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方法中的所有打印语句都会分散您的注意力。我考虑过将它们取出以便于阅读,但我希望它与我一直在运行的完全一样。

最佳答案

linearSearch(array, 30);


他们确实返回了一些东西。但是用返回值做点什么!

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


甚至更简单:

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


回应您的编辑

您需要启动left1。您正在执行整数除法,它永远不会达到零。因此,right卡在1上,并且left总是小于它。

关于java - 方法在Java中什么都不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16470042/

相关文章:

c++ - 如何按降序打印数组?

java - 使用Java中的Levenshtein距离改善搜索结果

python - Django全文搜索不匹配部分单词

php - 搜索多个值,按相关性排序(php + MySQL,无全文搜索)

java - 如何从缓冲读取器输入字符串?

java - 从 Java 访问 Visual FoxPro 数据库

java - 将已安装的小部件添加到Android Activity

java - 我应该编写单元测试来检查@NonNull契约(Contract)吗?

c++ - 如何在保留原始索引的同时对 vector 进行排序?

string - Haskell 如何对字符串排序?