2011-12-19 3 views
0

私は、プログラムがユーザから10の数字を読み込み、次に検索する番号を要求するJavaの宿題に取り組んでいます。数値をソートし(昇順)、配列に対して線形検索を行い、見つかった添え字か見つからないというメッセージを返します。リニアサーチは、添え字番号を返す必要があるときには見つかりません

下付き文字を返す必要がある場合でも、私のコードでは「見つからない」という応答が返されます。私のコードを見てもらえますか?私はif文を変更しようとしましたが、それは助けになりませんでした。私は検索とソートの方法の本の例を私ができる限り緊密に使ってきました(エラーを指摘することは自由です)。

ここにコードがあります。

package program11; 
import java.util.Scanner; 

public class ArraySearch { 

    public static void main(String[] args) { 
     double[] arrayBuild = new double[10]; 
     Scanner input = new Scanner(System.in); 

     for (int i = 0; i < arrayBuild.length; i++) { 
      System.out.print("Enter a number."); 
      arrayBuild[i] = input.nextDouble(); 
     } 

     System.out.print("Enter a number to search for "); 
     int objective = input.nextInt(); 

     linearCheck(arrayBuild, objective); 

     if ((objective >= 0) && (objective < arrayBuild.length)) { 
      System.out.println("Found at index: " + objective); 
     } else { 
      System.out.println("Not Found"); 
     } 
    } 

    public static void arraySort(double[] arrayBuild) { 
     for (int i = 1; i < arrayBuild.length; i++) { 
      double currentPoint = arrayBuild[i]; 
      int r; 
      for (r = i - 1; r >= 0 && arrayBuild[r] > currentPoint; r--) { 
       arrayBuild[r + 1] = arrayBuild[r]; 
      } 
      arrayBuild[r + 1] = currentPoint; 
     } 
    } 

    public static double linearCheck(double[] arrayBuild, int objective) { 
     for (int i = 0; i < arrayBuild.length; i++) { 
      if (objective == arrayBuild[i]) 
       return i; 
     } 
     return -1; 
    } 

} 

編集 - 新しいコード。配列にない数字を入力する場合を除いて、すべてが完全です。これは否定的な結果になります。たとえば、10 8 7 6 5 3 5 3 5 3 5 6と入力して11を検索すると、線形検索では-1、バイナリ検索では-11という結果が得られます。私はできるだけあなたのアドバイスを受けた。私は今何が逃していますか?

package program11; 
import java.util.Scanner; 
import javax.swing.JOptionPane; 
public class ArraySearch { 

    public static void main(String[] args) { 
     double[] arrayBuild = new double[10]; 
     Scanner input = new Scanner(System.in); 
     int reply = 2; 

for (int i = 0; i < arrayBuild.length; i++) { 
    System.out.print("Enter a number."); 
    arrayBuild[i] = input.nextDouble(); 
     } 
while (reply != 1) { 
     System.out.print("Enter a number to search for "); 
     double objective = input.nextDouble(); 

arraySort(arrayBuild); 
     double linearResult = linearCheck(arrayBuild, objective); 

if (objective >= 0) { 
     System.out.println("Linear search found at index: " + linearResult); 
       } 
else { 
     System.out.println("Not Found (linear)"); 
     } 
     double binaryResult = binaryCheck(arrayBuild, objective); 

if (objective >= 0) { 
System.out.println("Binary search found at index: " + binaryResult); 
       } 
else { 
    System.out.println("Not Found (binary)"); 
     } 
reply = JOptionPane.showConfirmDialog(null, "Continue?"); 
    } 
} 

public static void arraySort(double[] arrayBuild) { 
    for (int i = 1; i < arrayBuild.length; i++) { 
     double currentPoint = arrayBuild[i]; 
     int r; 
     for (r = i - 1; r >= 0 && arrayBuild[r] > currentPoint; r--) { 
      arrayBuild[r + 1] = arrayBuild[r]; 
     } 
     arrayBuild[r + 1] = currentPoint; 
    } 
    } 

public static double linearCheck(double[] arrayBuild, double objective) { 
    for (int i = 0; i < arrayBuild.length; i++) { 
     if (objective == arrayBuild[i]) 
      return i; 
    } 
return -1; 
    } 
public static double binaryCheck(double[] arrayBuild, double objective) { 
    int low = 0; 
    int high = arrayBuild.length - 1; 

    while (high >= low) { 
     int mid = (low + high)/2; 
     if (objective < arrayBuild[mid]) 
      high = mid - 1; 
     else if (objective == arrayBuild[mid]) 
      return mid; 
     else 
     low = mid + 1; 
    } 
return -low - 1; 
    } 
} 
+1

これは私が見る動作ではありません。配列をどこでソートしますか? –

+0

も私にとって役に立ちます – Chris

+0

arraySortメソッドの下で配列をソートします。それはあなたのために働いていますか?私が10個の数字を入力し、1が含まれていて、検索の目的として1を選択すると、私は見つからないという応答を得る。 =/ – user1082706

答えて

1

あなたはint型を検索したい場合は、私はまた、int型のarrayBuildになるだろう

  1. 私はあなたの問題を引き起こしているかわから正確にわからないんだけど、私はあなたのコードでいくつかの問題を参照してください同じように。それとも、あなたはどこにでも結果を格納していないlinearCheck呼び出すときには、倍増するobjectiveの種類を変更し、入力ダブル

  2. にユーザーを取得することにより、ダブルスのために検索することができますが、変数

    double foundLocation = linearCheck(arrayBuild, objective); 
    
    に割り当てる必要があります
  3. 文はあまり意味がない場合はあなた、あなたはlinearCheckの戻り値を使用してされるべきである

    if (foundLocation >= 0) { 
        System.out.println("Found at index: " + foundLocation); 
    } else { 
        System.out.println("Not Found"); 
    } 
    
  4. を次のようにあなたがいますarraySortあなたのメインのどこにでも電話しないでください

+0

ありがとう!助けて、私はそれを理解した。唯一の問題は、それが数字を返すことであり、私は下付き文字を取得する必要があります。私はもう少しそれを混乱させます。 – user1082706

+0

待ってください。うんありがとう! – user1082706

1

あなたは決して検索の価値を保存しません。あなたの目的はあなたの結果と同じではありません。

int result = linearCheck(arrayBuild, objective); 

    if ((result >= 0) && (result < arrayBuild.length)) { 
     System.out.println("Found at index: " + result); 
    } else { 
     System.out.println("Not Found"); 
    } 

この変更では、リニアチェックルーチンでintを返す必要があります。

これは、objectiveをダブルで整数にするのに加えています。

+0

ありがとう、私はあなたのアドバイスを取った – user1082706

関連する問題