-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathHomework05.java
More file actions
69 lines (51 loc) · 1.48 KB
/
Homework05.java
File metadata and controls
69 lines (51 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public class Homework05 {
//编写一个main方法
public static void main(String[] args) {
/*
随机生成10个整数(1_100的范围)保存到数组,
并倒序打印以及求平均值、求最大值和最大值的下标、
并查找里面是否有 8 Homework05.java
*/
int[] arr = new int[10];
//(int)(Math.random() * 100) + 1 生产 随机数 1-100
for(int i = 0; i < arr.length; i++) {
arr[i] = (int)(Math.random() * 100) + 1;
}
System.out.println("====arr的元素情况=====");
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println("\n====arr的元素情况(倒序)=====");
for(int i = arr.length -1; i >= 0; i--) {
System.out.print(arr[i] + "\t");
}
//平均值、求最大值和最大值的下标
//我们这里将需要一起完成
//
double sum = arr[0];
int max = arr[0];
int maxIndex = 0;
for(int i = 1; i < arr.length; i++ ) {
sum += arr[i]; //累积和
if( max < arr[i]) {//说明max不是最大值,就变化
max = arr[i];
maxIndex = i;
}
}
System.out.println("\nmax=" + max + " maxIndex=" + maxIndex);
System.out.println("\n平均值=" + (sum / arr.length));
//查找数组中是否有8->使用顺序查找
int findNum = 8;
int index = -1; //如果找到,就把下标记录到 index
for(int i = 0; i < arr.length; i++) {
if(findNum == arr[i]) {
System.out.println("找到数" + findNum + " 下标=" + i);
index = i;
break;
}
}
if(index == -1) {
System.out.println("没有找到数" + findNum );
}
}
}