我必须能够在5个长数组中找到最大的负值,无论该数组包含负整数还是正整数。我知道这很简单,但是如果您将我引向正确的方向,将不胜感激。
//Import Scanner so we can receive input from the user.
import java.util.Scanner;
public class Mess {
//Main Method
public static void main(String[] args) {
//Declare variables
int [] list = new int [6];
int num = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int max = 0;
//While loop to store in array
while (num<=5){
list [num]=input.nextInt();
num++;
}
//Arrays.sort(list);
//For loop to find max value
//list[num]= max;
System.out.print(max);System.out.print(max);
for (int i = 1; i < num; i++) {
if (list[i] > max){
max = list[i];
}
}
//Print out max value
System.out.print("The max value is " + max + " ");
input.close();
}
}
最初将max设置为Integer.MIN_VALUE
//Declare variables
int [] list = new int [6];
int num = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int max = Integer.MIN_VALUE;
//While loop to store in array
while (num<=5){
list [num]=input.nextInt();
num++;
}
for (int i = 0; i < num; i++) {
if (list[i] > max){
max = list[i];
}
}
//Print out max value
System.out.print("The max value is " + max + " ");
input.close();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句