0

メインに挿入検索があり、そこから BinarySearch メソッドを呼び出して、既に並べ替えられた配列内の数値を検索します。私はそれをコンパイルしようとしましたが、ここでエラーが発生しましたが、挿入ソートのメインです。

public class test5
{
 public static void main(String[] args)
 {

// original order or numbers
System.out.println("Original order of numbers:");
int nums[] = {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};

for(int x = 0; x < nums.length; x++){
System.out.print(nums[x] + " ");
}


// local variables
int unsortedValue; // The first unsorted value
int scan; // used to scan the array
int swapCount = 0;


// the other loop steps the index variable through 
// each subscript in the array, starting at 1. This
// is because element 0 is considered already sorted.
for(int index = 1; index < nums.length; index++)
{
    // The first element outside the sorted subset is
    // nums[index]. Store the value of this elementt
    // in unsortedValue.
    unsortedValue = nums[index];

    // Start the scan at the subscript of the first element
    // into its proper position within the sorted subset.
    scan = index;

    // Move the first element outside the sorted subset
    // into its proper position within the sorted subset.
    while(scan > 0 && nums[scan-1] > unsortedValue)
    {
        nums[scan] = nums[scan -1];
        scan--;

    }

    // Insert the unsorted value in its proper position
    // within the sorted subset.
    nums[scan] = unsortedValue;
    swapCount++;
        }

    //  print out results of swap and swapCount
    System.out.println();
    System.out.println("Insertion sort: ");

    for(int index = 0; index < nums.length; index++){   
            System.out.print(nums[index] + " ");
                        }

    System.out.println();
    System.out.println("The swap count is: " + swapCount);
    BinarySearch(nums);
    }

二分探索の方法はこちら

public static int BinarySerach(String[] array, String value)
{
Scanner keyboard = new Scanner(System.in);

System.out.print("Enter target: ");
int target = nums.nextInt();
int index = -1;
int left = 0;
int right = nums.length - 1; 
int middle;
while(left <= right){
    middle = (left + right)/2;
    if(nums[middle] == target){
        index = middle;
        break;
    } else if (nums[middle] > target) {
        right = middle - 1;
    }else{
        left = middle + 1;
    }
}
if(index == -1){
    System.out.println("element not found");
}else{
    System.out.println("element found at index " + index);
    }
}

}

表示されるエラーは、シンボルが見つかりません。

4

2 に答える 2

0

numsメソッド内で配列をローカルに宣言しmainたため、メソッドなど、他の場所ではアクセスできませんBinarySearch。スコープはmainメソッド内のみです。

すべてのメソッドの外で宣言してstatic、どのメソッドからでもアクセスできるようにします。

public class test5
{
    static int nums[] = {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};

    public static void main(String[] args)
    {
    ...

さらに、

  • (nums)nextIntを呼び出すことはできません。int[]オブジェクトで呼び出すつもりだったようですScannerint target = keyboard.nextInt();
  • どの引数も使用していませんBinarySearch- それらを削除してください。numsへの呼び出しを渡すことも削除しますBinarySearch
  • BinarySerachここではおそらくタイプミスですが、の代わりにここでメソッドを宣言しましたBinarySearch
  • メソッドから何BinarySearchも返していませんが、そのメソッドの戻り値を何にも割り当てていません。戻り値の型を作るだけvoidです。
于 2013-04-19T22:02:21.297 に答える
-1
import java.io.*;
class BinarySearch{
public static void main(String args[])throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader (isr);
int a[]={7, 10, 12, 34, 45,51, 60, 78, 81, 92};
int key, low=0, high=a.length-1, mid=0;
System.out.print("Enter the integer to search:");        key=Integer.parseInt(br.readLine());
while(low<=high){
mid=(low+high)/2;
if (key==a[mid])
break;
else if(key<a[mid])
high=mid-1;
else
low=mid+1;
}
if(low<=high)
System.out.println(key+" found at index "+mid);
else
System.out.println(key+" not found");
}
}``
于 2016-03-26T15:40:58.967 に答える