forked from coding-technology/JavaCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodTest.java
More file actions
58 lines (47 loc) · 1.55 KB
/
MethodTest.java
File metadata and controls
58 lines (47 loc) · 1.55 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
import java.util.Scanner;
public class MethodTest {
//3个学生, 平均分 和最高分。不能定义全局变量
public static void main(String[] args) {
int[] students = new int[3];//{0,0,0}
MethodTest test = new MethodTest();
test.myInput(students);
double avg = test.calcAvg(students);
int sum = test.calcSum(students);
int max = test.calcMax(students);
}
//student = {0,0,0}
public void myInput(int[] students) {
Scanner input = new Scanner(System.in);
System.out.println("请输入第一个学生成绩:");
students[0] = input.nextInt();//99
System.out.println("请输入第二个学生成绩:");
students[1] = input.nextInt();//88
System.out.println("请输入第三个学生成绩:");
students[2] = input.nextInt();//77
// students = new int[]{student1,student2,student3} ;
}
public double calcAvg(int[] students) {
// int sum = 0 ;
// for(int student:students){
// sum += student ;
// }
int sum = calcSum(students);
return sum * 1.0 / students.length; //299.0/3
}
public int calcSum(int[] students) {
int sum = 0;
for (int student : students) {
sum += student;
}
return sum;
}
public int calcMax(int[] students) {
int max = students[0];
for (int i = 1; i < students.length; i++) {
if (max < students[i]) {
max = students[i];
}
}
return max;
}
}