Java作业题

来源:百度知道 编辑:UC知道 时间:2024/09/24 13:24:39
编写一个成绩统计的java程序。先在数组中定义5个学生的成绩,再求出平均成绩,最后找出并输出最高分,用循环语句对5个学生的成绩排序,并按从小到大的顺序输出。

public class ScoreDeal {

public double getAvg(int[] nums) {
int temp = 0;
if(nums != null && nums.length > 0) {
for(int num : nums) {
temp += num;
}
return temp / nums.length;
}
else {
return -1;
}
}

public int getMax(int[] nums) {
int temp = 0;
if(nums != null && nums.length > 0) {
for(int num : nums) {
if(temp < num) {
temp = num;
}
}
return temp;
}
else {
return -1;
}
}

public String getSort(int[] nums) {
int i = 0;
int j = 0;
int temp = 0;
for (i = 0; i < nums.length; i++) {
for (j = i; j < nums.length - 1; j++) {
if (nums[i] > nums[j + 1]) {
temp = nums[j + 1];
nums[j + 1] = nums[i];
nums[i] = temp;
}
}
}

String result = "&qu