【问题标题】:Methods aren't returning anything in Java方法在 Java 中不返回任何内容
【发布时间】:2013-05-04 10:00:22
【问题描述】:

我用 5 个方法用 Java 编写了一个类。线性搜索,如果找到值则返回 true,如果未找到则返回 false。线性搜索 2,如果找到,则返回值的位置。二进制搜索。它也在数组中搜索值,打印 Int 数组,它一次打印 10 个数字,以及选择排序,它对数组进行排序,以便我可以进行二进制搜索。一切都编译得很好,但由于某种原因,我的方法都没有返回任何东西(除了 void printIntArray 方法)。

编辑:


谢谢,伙计们,我没有意识到我需要那个。出于某种原因,我认为它会自行返回值……不过,这是另一个问题。 binarySearch 方法似乎没有做任何事情。在打印语句“使用二进制搜索在随机数组中搜索 11”....之后,什么都没有打印出来。


编辑 2: 我的 binarySearch 方法不起作用,因为我不小心为两个 else 语句设置了 mid + 1(else if (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 方法中的所有打印语句都分散了注意力。我想把它们拿出来以方便阅读,但我希望它和我一直在运行它一样。

【问题讨论】:

  • 确保当你的方法返回某些东西时,某些东西会得到回报。即:VARIABLE = binarySearch(array,74)
  • 你的binarySearch函数调试了吗?
  • 请检查我的编辑。

标签: java sorting search methods


【解决方案1】:

您必须将排序/搜索方法调用放在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】:
    linearSearch(array, 30);
    

    他们确实返回了一些东西。但是对返回值做一些事情!

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

    甚至更简单:

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

    响应您的编辑

    您需要将left 发起到1。您正在执行整数除法,它永远不会达到零。因此right 卡在1 上,而left 总是小于它。

    【讨论】:

    • 克里斯,这太完美了。感谢您回复我的编辑。
    • 好吧,如果这就是一切,请务必将我的答案标记为正确:)
    【解决方案3】:

    他们返回他们应该做的,只是你选择不对他们返回的东西做任何事情。

    您可以通过将函数调用包装在System.out.println() 中来解决此问题,或者使用ret = yourfunction(params) 存储返回值并稍后显示ret

    【讨论】:

      【解决方案4】:

      它们不返回任何内容,因为您没有将返回值分配给任何变量。

      做这个:

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

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

      等等。

      【讨论】:

      • 我认为linearSearch 返回一个布尔值:)
      猜你喜欢
      • 2019-10-23
      • 2020-01-26
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 2016-07-31
      相关资源
      最近更新 更多