ACCPJAVAS1第14章示例四

    xiaoxiao2022-06-30  95

    public class shili4 { int sum = 0; double avg = 0; int[] score = new int[5]; //编写avg方法求总和和平均分 public double avg(int[] score) { for (int i = 0; i < score.length; i++) { sum += score[i]; } avg = sum / score.length; return avg; } //求最大值 public int max(int[] score) { int max = score[0]; for (int i = 0; i < score.length; i++) { if (max < score[i]) { max = score[i]; } } return max; } //求最小值 public int min(int[] score) { int min = score[0]; for (int i = 0; i < score.length; i++) { if (min > score[i]) { min = score[i]; } } return min; } } <pre name="code" class="java">import java.util.*; public class shili44 { public static void main(String[] args) { Scanner input = new Scanner(System.in); shili4 a = new shili4(); //依次将5名学生的成绩插入数组 int[] score = new int[5]; for (int i = 0; i < score.length; i++) { System.out.println("请输入选手的成绩"); score[i] = input.nextInt(); } a.avg(score); int x = a.max(score); int y = a.min(score); System.out.println("成绩总和为:" + a.sum); System.out.println("平均分为:" + a.avg); System.out.println("最大值为:" + x); System.out.println("最小值为:" + y); } }

    转载请注明原文地址: https://ju.6miu.com/read-1126214.html

    最新回复(0)