【问题标题】:Finding the largest number in an array using predefined java methods使用预定义的 java 方法查找数组中的最大数
【发布时间】:2021-03-06 09:31:27
【问题描述】:

这是我正在处理的代码 sn-p,我的目标是使用预定义的 java 方法从列表中找到最大值。

import java.util.*;

public class Test {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter a list of integers: ");
        int array[] = new int[10];

        for (int i = 0; i < array.length; i++) {
            array[i] = s.nextInt();
        }

        for (int j = 0; j < array.length; j++) {
            if (j < array.length) {
                int maximum = Math.max(array[j], array[j + 1]);
            }
        }

        System.out.println("Largest number of the list: " + maximum);
    }
}

我得到的错误如下:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
        maximum cannot be resolved to a variable

希望有人能帮我解决这个问题。

【问题讨论】:

    标签: java arrays variables methods max


    【解决方案1】:

    您在 for 循环块中定义 ma​​ximum 变量,使其成为局部变量,然后您尝试在其定义块之外访问此变量的值,这就是为什么Java 找不到这样的变量,因为它在该范围内不存在。试试这个:

    public class Test {
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            System.out.println("Enter a list of integers: ");
            int array[] = new int[10];
            int maximum = 0;
            for (int i = 0; i < array.length; i++) {
                array[i] = s.nextInt();
            }
    
            for (int j = 0; j < array.length; j++) {
                if (j < array.length) {
                    maximum = Math.max(array[j], array[j + 1]);
                }
            }
    
            System.out.println("Largest number of the list: " + maximum);
        }
    }
    

    尝试阅读 this link 以进一步了解范围和变量。

    【讨论】:

      【解决方案2】:

      如上所述,注意变量作用域:

      public class Test {
          public static void main(String[] args) {
              Scanner s = new Scanner(System.in);
              System.out.println("Enter a list of integers: ");
      
              int array[] = new int[10];
              for (int i = 0; i < array.length; i++) {
                  array[i] = s.nextInt();
              }
      
              int maximum = 0;
              for (int j = 0; j < array.length - 1; j++) {
                  maximum = Math.max(array[j], array[j + 1]);
              }
      
              System.out.println("Largest number of the list: " + maximum);
          }
      }
      

      【讨论】:

        【解决方案3】:
        1. 您可以使用IntStream.max() 方法找到int 基元流的最大元素:

          int[] arr = {4, 7, 5, 3, 2, 9, 2, 7, 6, 7};
          
          int max = Arrays.stream(arr).max().getAsInt();
          
          System.out.println(max); // 9
          
        2. 您可以使用Stream.max(Comparator) 方法查找Integer 对象流的最大元素:

          List<Integer> list =
                  Arrays.asList(6, 10, 2, 1, 6, 4, 5, 8, 9, 8);
          
          Integer max2 = list.stream()
                  .max(Comparator.comparingInt(Integer::intValue))
                  .get();
          
          System.out.println(max2); // 10
          

        另见:
        How to find first 5 highest value in a two dimensional array?
        Selection sort of array in Java

        【讨论】:

          【解决方案4】:

          您可以只迭代数字数组并跟踪看到的最大值:

          int largest = Integer.MIN_VALUE;
          
          for (int j=0; j < array.length; j++) {
              if (array[j] > largest) {
                  largest = array[j];
              }
          }
          

          注意:上面的 sn-p 假设输入数组中至少有一个数字。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-10-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-12
            • 1970-01-01
            相关资源
            最近更新 更多