java编写程序,实现用户输入5个整数,然后找到这五个数的最大值

编写程序,实现用户输入5个整数,保存到数组中,然后找到这五个数的最大值,和这个最大值所在数组中的下标。

package question.baidu;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] array = new int[5];
int max = 0;
int temp = 0;
for (int i = 0; i < 5; i++) {
System.out.println("请输入第" + (i + 1) + "数:");
array[i] = scanner.nextInt();
if (temp < array[i]) {
temp = array[i];
max = i;
}
}
System.out.println("最大的数的数组下标为:" + max + "最大的数是:" + array[max]);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-06-13
先使用scanner输入5个数,上面的不写了,主要下面的比较过程
可以采用三目运算符
int max;
int min;
max=(a>b)?a:b;

max=(max>c)?max:c;
max=(max>d)?max:d;
max=(max>e)?max:e;
System.out.println(max);
min=(a<b)?a:b;
min=(min<c)?min:c;
min=(min<d)?min:d;
min=(min<e)?min:e;
System.out.println(min);
相似回答