2016-02-04 107 views
0

我试图创建一个二进制搜索程序,返回我的“位置”的值(数组的索引,其中我们正在搜索的值位于)但是,作为该作业的一部分,我不允许打印直接从我的binarySearch方法。有没有人知道我可以从我的主要方法打印搜索结果的方式。谢谢!二进制搜索,返回键值。

import java.util.Scanner; 
import java.util.Arrays; 
// in - Scanner 
// value - String input from user 
// v - Integer input from user 
// counter - Integer for counting down elements remaining 
// i - counting slave 
// key - object we are searching for 
public class HelloWorld 
{ 
//******************************************************************************************************************** 
    public static void main(String[]args) 
    { 
     Scanner in = new Scanner(System.in);//create reference to new scanner 
     System.out.println("Enter the integer size of the array you wish to create."); 
     String value = in.nextLine();//value = size of the array 
     int v = Integer.parseInt(value);//parse to int 
     String[]sampleArray;//creates ref to array 
System.out.println("Debug: int v = "+v); 
     sampleArray=new String[v];//size of the array 
     int counter = v; 
      for(int i = 0; i < sampleArray.length; i++,v--)//until the array is full.. 
       { 
        if(i == sampleArray.length-1) 
         { 
          System.out.println("Enter the last element of the array."); 
          sampleArray[i] = in.nextLine(); 
         } 
        else 
         { 
          System.out.println("Enter an element to the array. You have to add "+v+" more elements."); 
          sampleArray[i] = in.nextLine(); 
         } 
       } 
     System.out.println("Enter Key."); 
     String key2 = in.nextLine(); 
//******************************************************************************************************************** 
    for (int i = 1; i < sampleArray.length; i++) 
     { 
      String key = sampleArray[i]; 
      int position = i; 
       while (position > 0 && key.compareTo(sampleArray[position-1]) < 0) 
        { 
         sampleArray[position] = sampleArray[position-1]; 
         position--; 
        } 
       sampleArray[position] = key; 
     } 
//******************************************************************************************************************** 
int i = sampleArray.length-1; 
    while(i >= 0) 
     { 
      System.out.print(sampleArray[i]+" "); 
      i = i - 1; 
     } 
     System.out.println("done sorting.."); 
    binarySearch(sampleArray,0,sampleArray.length,key2); 
    System.out.println(binarySearch(position));//**HERE IS MY ERROR** 
    } 
//******************************************************************************************************************** 
public static int binarySearch(String[] array, int lowerbound, int upperbound, String key) 
    { 
    int position; 

    position = (lowerbound + upperbound)/2; 

    while((array[position].compareTo(key) == 0) && (lowerbound <= upperbound)) 
    { 
     if (array[position].compareTo(key) > 0)   // If the number is > key, .. 
     {             // decrease position by one. 
       upperbound = position - 1; 
     } 
       else 
     { 
       lowerbound = position + 1;     // Else, increase position by one. 
     } 
     position = (lowerbound + upperbound)/2; 
    } 
     if (lowerbound <= upperbound) 
     { 
      return position; 
     } 
     else 
     { 
      return-1; 
     } 
    } 
} 
+0

这是否编译? 'binarySearch'有四个参数,而不是一个。 – markspace

+0

是的,只有我得到的错误是找不到符号变量“位置”我评论它,“这是我的错误”,所以你可以找到它。 @markspace – robby

+1

你的函数调用需要更多的参数。此外,还有上下文丢失,或者您正尝试使用一个当前根本不可用的变量。 – gpgekko

回答

1

您的代码是错误的。

  • 您还没有声明position变量。
  • 由于函数需要4个参数,因此不能调用binarySearch(position)

仅仅通过猜测通过binarySearch(position)要打印在那里发现的元素的位置,我建议以下解决方案:

更改您的这部分代码:

binarySearch(sampleArray,0,sampleArray.length,key2); 
System.out.println(binarySearch(position)); 

要:

int position = binarySearch(sampleArray,0,sampleArray.length,key2); 
System.out.println(position); 

这工作正常,我认为这是你想要的。

+0

你是上帝,是的,这是我想要的,完美的作品。 – robby