2017-06-26 6 views
0

私のコードでは、アレイ内のその番号に到達するのに必要な回数だけ、「要素が配列に見つかりません」という文字が印刷されています。例外処理の修正

マイ割付:

指定された整数値の整数のアレイ(下記方法ヘッダーを開始のヘルプを参照)を検索する方法でJavaプログラムを作成します。配列に指定された整数が含まれている場合、メソッドはその配列のインデックスを返す必要があります。そうでない場合、メソッドは「配列に要素が見つかりません」という例外をスローし、正常終了させる必要があります。あなたが作る配列と "針"のユーザ入力を使って、mainのメソッドをテストします。

パブリックstatic int型returnIndex(INT []干し草、INT針){これまで

マイコード:

import java.util.Scanner; 
public class Assignment1 { 

public static void main(String[] args) { 
    int[] haystack = { 4,5,6,7,12,13,15,16,22,66,99,643 };   
    Scanner sc = new Scanner(System.in); 
    System.out.println("Enter a number in the array: ");     
    int needle = sc.nextInt(); 
    returnIndex(haystack,needle); 

    } 
public static int returnIndex(int[] haystack, int needle) { 
     for (int n = 0; n < haystack.length; n++) { 
      if (haystack[n] == needle) 
       return n; 
      else 
       System.out.println("Element not found in array"); 
     } 
     return -1; 
    } 
} 

マイ出力:

Enter a number in the array: 
7 
Element not found in array 
Element not found in array 
Element not found in array 

又は

Enter a number in the array: 
15 
Element not found in array 
Element not found in array 
Element not found in array 
Element not found in array 
Element not found in array 
Element not found in array 
+0

ヒントのために働く機能してください試してみてください); 'はループの内部に属しません。 –

+0

'else'ブロックの' System.out'を削除し、 'returnIndex'メソッドが返った後で結果を確認してください。問題の説明に従って結果を処理します。 – andih

+0

あなたの 'for(int n = 0; n Sergey

答えて

4

あなたは同じoutpuを得ていますループを中断していないので、複数回繰り返します。配列全体に要素がない場合は、System.out.println("Element not found in array");をforループの外側に移動します。以下は完全な例です。見てください。

以下のコードは、どこの要素が配列に見つからない場合にループを壊していますか?

public class SOTest { 
    public static void main(String[] args) { 
     int[] haystack = { 4, 5, 6, 7, 12, 13, 15, 16, 22, 66, 99, 643 }; 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter a number in the array: "); 
     int needle = sc.nextInt(); 
     int index = returnIndex(haystack, needle); 
     if(index!=-1) // print index only if element is in array. 
     System.out.println("Element found at index : " + index); 

    } 

    public static int returnIndex(int[] haystack, int needle) { 
     for (int n = 0; n < haystack.length; n++) { 
      if (haystack[n] == needle) 
       return n; 
     } 
     System.out.println("Element not found in array"); 
     return -1; 

    } 
} 

編集: - プログラムの正しさを検証するために実行可能なコードとサンプル入力と出力を追加しました。また、要素が配列内にあり、-1のインデックス手段が配列内にない場合は、インデックスを出力するロジックを追加しました。

番号の入力と出力: - 120は配列内にありません。配列である数5用

Enter a number in the array: 
120 
Element not found in array 

、出力がbbelow次のようになります -

あなたはループの外プリントラインを配置する必要があり
Enter a number in the array: 
5 
Element found at index : 1 
+0

5を入力してください。 –

+0

@KevinAnderson、変更コード –

+0

を追加しましたあなたのご意見ありがとうございますが、配列に含まれていない番号を入力すると何らかの理由で "...配列ではありません" "...見つかったインデックス:-1"どうすれば修正できますか? – michael

2

public static int returnIndex(int[] haystack, int needle) { 
     for (int n = 0; n < haystack.length; n++) { 
      if (haystack[n] == needle) 
       return n; 
     } 

     System.out.println("Element not found in array"); 
     return -1; 
} 

あなたが得るためならば要素を返してと返すと、下部が実行されません。ループの中に置くと、探しているものと一致しないすべての要素に対して、見つからないテキストが印刷されます。

より良い解決策は、以下のように、メソッドの外の要素を印刷するには、次のようになります。

public static int returnIndex(int[] haystack, int needle) { 
    for (int n = 0; n < haystack.length; n++) { 
     if (haystack[n] == needle) 
      return n; 
    } 
    return -1; 
} 

public static void main(String[] args) { 
    ...    
    int needle = sc.nextInt(); 
    if (returnIndex(haystack,needle) == -1){ 
      System.out.println("Element not found in array"); 
    } 
    else { 
      System.out.println("Element found in array"); 
    } 
} 
0

はリターン-1上のSystem.outを入れてみてください、この方法は、あなたは、「要素を印刷しないではありません選択された番号が見つからないたびに「配列で見つかりました。`のSystem.out.println( "配列には見られない要素":

0

この

ちょうどreturnIndexの小さな変化が、それはあなた

public static int returnIndex(int[] haystack, int needle) { 
    for (int n = 0; n < haystack.length; n++) { 
     if (haystack[n] == needle) { 
      return n; 
     } else { 
      if(n == haystack.length - 1) 
      System.out.println("Element not found in array"); 
     } 
    } 
    return -1; 
}