-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray2.java
More file actions
65 lines (59 loc) · 1.61 KB
/
Copy pathArray2.java
File metadata and controls
65 lines (59 loc) · 1.61 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
package wylearn.Array;
import java.util.Scanner;
/**
*Java 语言进阶 主讲人:翁恺 时间:2018/9/1
* 内容:数组
*
* 性质
* 数组是一种容器;
* 元素数据类型相同;
* 创建后不能改变大小
*
* 机制:最小的下标是0,最大的下标是数组的元素个数-1
* 编译器不会检查下标是否有效,即数组下标越界也不会当时就提示,运行后才提示
* 运行时出现无效的下标,会抛出异常:数组下标越界
*
* 定义数组变量:
* <类型>[] <名字> = new <类型>[元素个数];
* example:int[] grades = new int[100];
* double[] averages = new double[20];
* 元素个数必须是整数
* 元素个数必须给出
* 元素个数可以是变量------>Array2
*
* 数组常用到的成员变量:length
*/
/**
* 1.输入一串数字,找出大于平均值的数列
* 2.数组的元素个数由用户输入
*/
public class Array2 {
public static void main (String[] args){
Scanner in = new Scanner(System.in);
// 功能:从键盘接收数据
double sum = 0;
int cnt = 0;
cnt = in.nextInt();
//用户先明确要输入多少个
if (cnt > 0){
int[] numbers = new int[cnt];
//定义数组,创建cnt个长度的int类型数组
for (int i = 0; i < cnt; i++){
numbers[i] = in.nextInt();
//将输入的x赋值数组中
sum += numbers[i];
}
double average = sum/cnt;
for (int i = 0; i < numbers.length; i++){
/*
*遍历数组
*/
if (numbers[i] > average){
//使用数组的元素
System.out.println(numbers[i]);
}
}
System.out.println(sum/cnt);
}
}
}