【发布时间】:2021-11-24 18:56:40
【问题描述】:
代码如下:
import java.util.Collections;
import java.util.Scanner;
public class task2 {
public static void main(String[] args) {
int n;
Scanner numbers = new Scanner(System.in);
System.out.print("Enter the number of elements you want to store: ");
// reading the number of elements from the that we want to enter
n = numbers.nextInt();
// creates an array in the memory of length 10
int[] array = new int[10];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
// reading array elements from the user
array[i] = numbers.nextInt();
}
System.out.println("Array elements are: ");
// accessing array elements using the for loop
for (int i = 0; i < n; i++) {
System.out.println(array[i]);
}
System.out.println("The max value is: " + Collections.max(numbers));
}
}
我收到一条错误消息,提示“集合类型中的方法 max(Collection extends T>) 不适用于参数(扫描器)”
如果有人可以帮忙,那就太好了,请记住,我是 Java 的绝对初学者,所以请原谅我犯的任何愚蠢的错误。
【问题讨论】:
-
numbers是Scanner。Scanner的max是什么? -
我正在尝试从我在数组中输入的输入中获取最大值
-
@AMalak 那么你需要对数组做一些事情,而不是你用来从输入中读取值的扫描仪。
标签: java arrays collections java.util.scanner