2017-11-20 5 views
0

この問題は私には:与えられた配列をテストし、そこに番号5があるかどうかを確認する必要があります。 5がある場合はFalseを返します。それ以外の場合はtrueを返します。私はそれを働かせました...まあ、私はそれが効率的な方法ではないことを知っています。唯一の問題は、ケース1とケース6の場合、私はError java.lang.ArrayIndexOutOfBoundsExceptionを得ています。なぜかわからないのです。残りはうまく動作します。配列に特定の値があるかどうかを調べるにはどうすればよいですか?

import java.util.*; 

public class testCases 
{ 
    public static void main(String[] args) 
    { 
     Scanner kb = new Scanner(System.in); 

     System.out.print("Which case do you wish to test? "); 

     int testCase = kb.nextInt(); 

     switch(testCase) 
     { 
      case 1: 
        System.out.println("noFives(1, 2, 3, 4) = " + noFives(1, 2, 3, 4, 6)); 
        break; 

      case 2: 
        System.out.println("noFives(1, 2, 3, 4, 5) = " + noFives(1, 2, 3, 4, 5)); 
        break; 

      case 3: 
        System.out.println("noFives(1, 2, 5, 3, 4) = " + noFives(1, 2, 5, 3, 4)); 
        break; 

      case 4: 
        System.out.println("noFives(5, 1, 2, 3, 4) = " + noFives(5, 1, 2, 3, 4)); 
        break; 
      case 5: 
        System.out.println("noFives(27, 82, 4, 71, 6, 23, 9, 18) = " + noFives(27, 82, 4, 71, 6, 23, 9, 18)); 
        break; 

      case 6: 
        System.out.println("noFives(0) = " + noFives(0)); 
        break; 

      case 7: 
        System.out.println("noFives(5) = " + noFives(5)); 
        break; 

      default: 
        System.out.println("noFives() = " + noFives()); 

     } 

    } 

    public static boolean noFives(int ... n) 
    { 

     if(n.length == 0) 
     return true; 

     else if(n[0] == 5 || n[1] == 5 || n[2] == 5 || n[3] == 5 || n[4] == 5 || n[5] == 5 || n[6] == 5 || n[7] == 5) 
     return false; 

     else 
     return true; 

    } 

} 
+0

'Arrays.stream(アレイ).filter(X - > X == 5).collect(Collectors.toList).size()== 0 '? – alfasin

+0

@apples forループを使用して配列を繰り返し処理するだけで簡単です –

答えて

0

代わりの配列私はあなたが、あなたは簡単にいくつかの要因があなたの実装の一貫性のない動作に遊ぶこの提供機能

mList.contains(specific object)

0

を使用することができます一覧

を使用することをお勧め:

  1. ||演算子の短絡。すなわち、noFives()の場合、n [0] == 5の場合、他の用語はテストされません。

  2. javaでは、配列の終わりを超えて要素にアクセスすると、OutOfBounds例外が発生します。

||操作の連鎖が短絡配列の外のインデックスにアクセスする前にいないときあなたは例外を取得します。事例1と6はこの短絡を引き起こすことはない。

0

ループを使用して、変化する長さをnにする必要があります。

import java.util.*; 

public class testCases 
{ 
    public static void main(String[] args) 
    { 
     Scanner kb = new Scanner(System.in); 

     System.out.print("Which case do you wish to test? "); 

     int testCase = kb.nextInt(); 

     switch(testCase) 
     { 
      case 1: 
        System.out.println("noFives(1, 2, 3, 4) = " + noFives(1, 2, 3, 4, 6)); 
        break; 

      case 2: 
        System.out.println("noFives(1, 2, 3, 4, 5) = " + noFives(1, 2, 3, 4, 5)); 
        break; 

      case 3: 
        System.out.println("noFives(1, 2, 5, 3, 4) = " + noFives(1, 2, 5, 3, 4)); 
        break; 

      case 4: 
        System.out.println("noFives(5, 1, 2, 3, 4) = " + noFives(5, 1, 2, 3, 4)); 
        break; 
      case 5: 
        System.out.println("noFives(27, 82, 4, 71, 6, 23, 9, 18) = " + noFives(27, 82, 4, 71, 6, 23, 9, 18)); 
        break; 

      case 6: 
        System.out.println("noFives(0) = " + noFives(0)); 
        break; 

      case 7: 
        System.out.println("noFives(5) = " + noFives(5)); 
        break; 

      default: 
        System.out.println("noFives() = " + noFives()); 

     } 

    } 

    public static boolean noFives(int ... n) 
    { 

     if(n.length == 0) 
     return true; 

     else { 
      for(int i=0; i < n.length; i++){ 
       if(n[i] == 5) 
        return false; 
      } 
     } 

     return true; 

    } 

} 
0

これは、パラメータによるものですが、あなたは5つのパラメータ

case 1: 
       System.out.println("noFives(1, 2, 3, 4) = " + noFives(1, 2, 3, 4, 6)); 
       break; 

を渡しているcase文1のための方法 noFives(int.. n) に送られて、あなたのコードの書き換えのに対し noFives(int... n)方法であなたはより多くのインデックスをチェックしていますしたがって、ArrayIndexOutOfBoundsException

else if (n[0] == 5 || n[1] == 5 || n[2] == 5 || n[3] == 5 || n[4] == 5 || n[5] == 5 || n[6] == 5 || n[7] == 5) 

私は方法を変更することをお勧めしますVES(INT ..n)等:

public static boolean noFives(int... n) { 

     if (n.length == 0) 
      return true; 

     for (int i = 0; i < n.length ; i++) { 
      if (n[i] == 5){ 
       return false; 
      } 
     } 
     return true; 
    } 
関連する問題