随机生成是个数字(1~100),保存到数组里,并倒序打印以及求平均值、最大值的下标、并查找里面是否有 8;
public class homeWork05 {
public static void main (String[] args){
int arr[] = new int[10];
for(int i = 0; i < arr.length; i++){
arr[i] = (int)(Math.random()*100) + 1;
}
System.out.println("---arr de zhi -------");
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + "\t");
}
System.out.println("\n ---daoxu-------");
for(int i = arr.length - 1; i >= 0; i--){
System.out.print(arr[i] + "\t");
}
double sum = arr[0];
double average = 0;
int max = arr[0];
int intIndex = 0;
for(int i = 1; i < arr.length;i++){
if(max < arr[i]){
max = arr[i];
intIndex = i;
}
sum += arr[i];
}
average = (sum / arr.length) ;
System.out.print("\n" + "max = " + max + "\t intIndex" + intIndex);
System.out.print("\n" + "sum = " + sum + "\t average" + average);
int findNum = 8;
int index = -1;
for(int i = 0; i < arr.length; i++){
if(arr[i] == findNum){
index = i;
System.out.println("zhao dao shu zi " + findNum + "xiao biao wei " + i);
break;
}
}
if(index == -1){
System.out.println("\n mei zhao dao " + findNum);
}
}
}