首页 > 其他分享 >数组操作索引得结果

数组操作索引得结果

时间:2022-11-14 14:45:55浏览次数:70  
标签:minIndex studentScores studentNames System 索引 数组 println 操作 out

public class Demo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("学生人数:");
        int n = scanner.nextInt();

        double[] studentScores = new double[n];
        String[] studentNames = new String[n];
        int i = 0;
//录入姓名分数 for (i = 0; i < n; i++) { System.out.println("请输入学生姓名:"); String studentName = scanner.next(); System.out.println("请输入" + studentName + "的成绩:"); double inputScore = scanner.nextDouble();
//存入到数组中 studentScores[i] = inputScore; studentNames[i] = studentName; }
//总分和平均分 double sum = 0; for (i = 0; i < n; i++) { System.out.println(studentNames[i] + "的成绩:" + studentScores[i]); } for (double score : studentScores) { sum += score; } System.out.println("总分为:" + sum + "\t平均分为:" + sum / n);
//最高分和最低分
//假设数组中第一个元素值为最大值或最小值 int maxIndex = 0; int minIndex = 0;
//依次比较 for ( i = 0; i < studentScores.length; i++) { if (studentScores[i] > studentScores[maxIndex]) { maxIndex = i; } if (studentScores[i] < studentScores[minIndex]) { minIndex = i; } } System.out.println("最高分:" + studentScores[maxIndex] + "\t姓名:" + studentNames[maxIndex]); System.out.println("最低分:" + studentScores[minIndex] + "\t姓名:" + studentNames[minIndex]); } }

 

标签:minIndex,studentScores,studentNames,System,索引,数组,println,操作,out
From: https://www.cnblogs.com/19981206-zxb/p/16888977.html

相关文章