题目要求

解题思路
这道题我没找到简便的Java方法,所以只能多几行啦
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] list = new int[n];
int maxIndex = 0;
for (int i = 0; i < n; i++) {
list[i] = in.nextInt();
if (list[i] > list[maxIndex]) {
maxIndex = i;
}
}
System.out.println(list[maxIndex] + " " + maxIndex);
}
}
本文介绍了一个Java程序,用于寻找输入数组中的最大值及其对应的索引位置。通过使用Scanner类读取用户输入的数据,并遍历数组来比较每个元素,最终输出最大值及其索引。

2万+

被折叠的 条评论
为什么被折叠?



