2017-01-22 9 views
0

これを実行して、同じ番号の最長シーケンスを出力するようにしています。私が得るエラーは、それがクラスまたは列挙型が予想されることを私に伝えることです。ここに私のコードです:配列リストの中で最も長い配列番号

public class D4 { 
    private static int getLongestRun(int[] array) { 
     int count = 1; 
     int max = 1; 
     for (int i = 1; i < array.length; i++) { 
      if (array[i] == array[i - 1]) { 
       count++; 
      } 
      else { 
       count = 1; 
      } 
      if (count > max) { 
       max = count; 
      } 
     } 
    } 

    public static void main(String[] args) { 
     int[] array = new int[]{5, 6, 6, 45, 2, 2, 2}; 
     System.out.println(getLongestRun(array)); 
    } 
} 

答えて

0

これはコメントとして所属していますが、私はあなたにそれが明確であるように完全なコードを与えます。ちょうどあなたのgetLongestRun()メソッドの最後にmaxを返す:

private static int getLongestRun(int[] array) { 
    int count = 1; 
    int max = 1; 

    for (int i = 1; i < array.length; i++) { 
     if (array[i] == array[i - 1]) { 
      count++; 
     } 
     else { 
      count = 1; 
     } 
     if (count > max) { 
      max = count; 
     } 
    } 

    // you forgot to return the length of the longest sequence to the caller 
    return max; 
} 
1

機能getLongestRun()return max;の文が欠落しています。

関連する問題