有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java数组列表中相同数字的最长序列

我试着执行这个,这样它就能打印出相同数字的最长序列。我刚刚编辑了它,但它告诉我放置一个返回声明。这是我的代码:

public class A1Q3 {
    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, -9, 56};
         System.out.println(getLongestRun(array));
     }    
}

共 (0) 个答案