2013-04-19 3 views
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); 
    } 
} 

} 내가 기호를 찾을 수 있습니다 얻을

오류에 대한 방법입니다.

+0

전체 스택 추적을 게시하십시오. 그 오류는 그 곳에서 자명하다. – torquestomp

+0

흠 스택 추적. 그게 무슨 뜻인지 모르겠다. Jgrasp와 관련이 있습니까? –

답변

0

main 메소드 내부에 nums 배열을 로컬로 선언 했으므로 다른 어느 곳에서도 액세스 할 수 없습니다. 귀하의 BinarySearch 방법. 범위는 main 메서드 내에서만 있습니다. 또한

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) 
    { 
    ... 

,

  • 당신은 int[] (nums)에 nextInt를 호출 할 수 없습니다 : 그것은 어떤 방법에서 액세스 할 수 있도록

    모든 방법이 static 외부를 선언합니다. 너 은 Scanner 개체라고합니다. int target = keyboard.nextInt();

  • BinarySearch에 대한 인수를 사용하고 있지 않습니다. 제거하십시오. BinarySearch으로 전화를 걸어 nums을 삭제합니다.
  • 아마도 여기에 오타가 있지만 BinarySearch 대신 여기에 방법을 선언했습니다. BinarySerach.
  • BinarySearch 메서드에서 아무 것도 반환하지 않지만 해당 메서드의 반환 값을 지정하지 않습니다. 반환 유형을 void으로 지정하십시오.
+0

감사합니다. 이러한 변경을 시도해 보겠습니다. –

+0

반품을 지정하는 것이 무슨 뜻인지 잘 모르겠습니다. 나는 그것을 어디에 두어야하는지 혼란 스럽다. 그리고 그것이 틀림없는 거짓과 같은 것이어야합니다. ?? –

-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"); 
} 
}`` 
+0

안녕하세요 gamerboy, 스택 Exchange에 오신 것을 환영하며 답변 해 주셔서 감사합니다. 왜 이것이 어떻게 또는 왜 이것이 문제를 해결하는지에 대한 약간의 설명으로 답을 넓힐 수 있다고 생각하십니까? 또한 코드를보다 쉽게 ​​읽을 수 있도록 포맷 할 수 있다면 훨씬 더 좋을 것입니다. 감사. – Kezz101

+0

질문하기 –

관련 문제