[英]Printing Index number of array
I am trying to print an output that would find the max number and also print the index number of the array as well. 我正在尝试打印输出,该输出将找到最大数量,并且还打印数组的索引号。 I am able to print the max number but I am unable to print the index number. 我可以打印最大数量,但无法打印索引编号。
public static int findMax(int[] allNumbers) {
int maxValue = allNumbers[0];
for (int i = 1; i < allNumbers.length; i++) {
if (allNumbers[i] > maxValue) {
maxValue = allNumbers[i];
}
}
return (int) maxValue;
}
my array: 我的数组:
int[] allNumbers = new int[inpNum];
I can call the max number with the following findMax(number)
我可以使用以下findMax(number)
调用最大数量
You can return an array of the max value and index: 您可以返回最大值和索引的数组:
public static int[] findMax(int[] allNumbers) {
int maxValue = allNumbers[0];
int index = 0;
for (int i = 1; i < allNumbers.length; i++) {
if (allNumbers[i] > maxValue) {
maxValue = allNumbers[i];
index = i;
}
}
return new int[] {maxValue , index};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.