一维数组的问题
我想知道一维数组中,怎样求其中的最大值,最小值以及差值···请教指点一些代码
我简单写了一个:
public class Suibianxiexie {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] score = { 4, 6, 5, 7, 8, 9, 4, 3, 9, 1 };
int min = score[0];
int max = score[0];
int m = 0, n = 0;
for (int i = 0; i <= score.length - 1; i++) {
if (score[i] > max) {
max = score[i];
m = i;
} else if (min > score[i]) {
min = score[i];
n = i;
}
}
System.out.print("最大数为:" + max + "下标为:" + m + "\n最小数为:" + min + "下标为:"
+ n);
}
}