问题
Here is the code snippet that I am working on and my goal is to find the largest value from the list using predefined java methods.
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);
}
}
The error that I am getting is the following:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
maximum cannot be resolved to a variable
I hope someone can help me resolve this.
回答1:
You may just iterate the array of numbers and keep track of the largest value seen:
int largest = Integer.MIN_VALUE;
for (int j=0; j < array.length; j++) {
if (array[j] > largest) {
largest = array[j];
}
}
Note: The above snippet assumes that you have at least one number in the input array.
回答2:
As said above, pay attention to the variables scope:
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:
You're defining your maximum variable inside the for loop block, making it a local variable, then you're trying to access the value on this variable outside of its definition block, that is why Java cannot find such variable, because it does not exist on that scope. Try This:
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);
}
}
Try reading this link for a further explanation on scopes and variables.
回答4:
You can use IntStream.max() method to find the maximum element of a stream of
int
primitives:int[] arr = {4, 7, 5, 3, 2, 9, 2, 7, 6, 7}; int max = Arrays.stream(arr).max().getAsInt(); System.out.println(max); // 9
You can use Stream.max(Comparator) method to find the maximum element of a stream of
Integer
objects: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
See also:
• How to find first 5 highest value in a two dimensional array?
• Selection sort of array in Java
来源:https://stackoverflow.com/questions/64972290/finding-the-largest-number-in-an-array-using-predefined-java-methods