问题遇到的现象和发生背景
求指点一下,为什么maxindex和minindex在if语句里赋值了但是还是初始化失败
问题相关代码import java.util.Scanner;
class Array{
public static void main(String[] args){
int ary[] = new int[10];
int sum = 0;
int number;
float average;
int max=0;
int min;
int maxindex;
int minindex;
for (int i =0;i< ary.length;i++){
number = (int)(Math.random()*99 + 1);
ary[i] = number;
sum += number;
}
average = sum/ary.length;
for (int i =0;i<ary.length;i++){
if (ary[i]>max){
max = ary[i];
maxindex = i;
}
}
min = max;
for (int i =0;i<ary.length;i++) {
if (ary[i] < min) {
min = ary[i];
minindex = i;
}
}
int temp;
for (int i=0;i< ary.length;i++){
for (int j=i+1;j< ary.length;j++){
if (ary[i]<ary[j]){
temp = ary[i];
ary[i] = ary[j];
ary[j] =temp;
}
}
}
for (int i=0;i< ary.length;i++){
System.out.println(ary[i]);
}
System.out.println("最大值为:"+ max+ "它的下标是" + maxindex);
System.out.println("最小值为:"+ min+ "它的下标是" + minindex);
System.out.println("平均值为:"+ average);
}
}
报错内容
Array.java:51: 错误: 可能尚未初始化变量maxindex
System.out.println("最大值为:"+ max+ "它的下标是" + maxindex);
^
Array.java:52: 错误: 可能尚未初始化变量minindex
System.out.println("最小值为:"+ min+ "它的下标是" + minindex);