繁体   English   中英

正确格式化我的输出

[英]Formatting my Output Correctly

我正在尝试让我的代码显示教师班级的最高分数,最低分数和平均分数。

我希望它显示如下:

最高分:94.7,杰克

最低分数:80.2,艾米丽

平均分数:85.6

最高成绩和平均成绩有效,最低成绩则无效。 这是我到目前为止的内容:

import java.util.*;

public class Grades {
    public static void main(String[] args) {
        ArrayList<Integer> bestStudentPosition = new ArrayList<Integer>();
        ArrayList<Integer> worstStudentPosition = new ArrayList<Integer>();
        Scanner input = new Scanner(System.in);
        System.out.print("How many students are in your class? ");
        int totalStudents = Integer.parseInt(input.nextLine());     
        String[] names = new String[totalStudents];
        double[] scores = new double[totalStudents]; 
        double maxGrade = 0;
        double minGrade = scores[0];
        double avg = 0;
        double sum = 0;
        for(int i = 0; i < totalStudents; i++){
              System.out.print("Name: ");
              names[i] = input.next();
              System.out.print("Score: ");
              scores[i] = input.nextDouble();
              sum += scores[i];

              if (scores[i] > maxGrade) {
                  bestStudentPosition.clear(); 
                  maxGrade = scores[i];
                  bestStudentPosition.add(new Integer(i));
              } 
              else if (scores[i] == maxGrade) {
                   bestStudentPosition.add(new Integer(i)); 
              }
              if (scores[i] < minGrade) {
                  worstStudentPosition.clear(); 
                  minGrade = scores[i];
                  worstStudentPosition.add(new Integer(i));
              } 
              else if (scores[i] == minGrade) {  
                   worstStudentPosition.add(new Integer(i)); 
              }

         }

         avg = sum/totalStudents;  
         System.out.print("Highest score: ");
         for (Integer position : bestStudentPosition) { 
             System.out.println(maxGrade + ", " + names[position]);
         }
         System.out.print("Lowest score: ");
         for (Integer position : worstStudentPosition) { 
              System.out.println(minGrade + ", " + names[position]);
         }

         System.out.printf("Average: %3.2f", avg);

    }
}

任何帮助,将不胜感激。

scores[i] < minGrade

其中minGrade最初为0,并且您永远不会为其分配除0之外的任何值。而且,这仅在等级小于0时才有效。

因此,可能您需要做的是,

import java.util.ArrayList;
import java.util.Scanner;

public class Grades {
    public static void main (String[] args) {
        ArrayList<Integer> bestStudentPosition  = new ArrayList<Integer>();
        ArrayList<Integer> worstStudentPosition = new ArrayList<Integer>();
        Scanner            input                = new Scanner(System.in);
        System.out.print("How many students are in your class? ");
        int      totalStudents = Integer.parseInt(input.nextLine());
        String[] names         = new String[totalStudents];
        double[] scores        = new double[totalStudents];
        double   maxGrade      = 0;
        double   minGrade      = 0;
        double   avg           = 0;
        double   sum           = 0;
        for (int i = 0; i < totalStudents; i++) {
            System.out.print("Name: ");
            names[i] = input.next();
            System.out.print("Score: ");
            scores[i] = input.nextDouble();
            sum += scores[i];
            if (i == 0) {
                minGrade = scores[0];
            }
            if (scores[i] > maxGrade) {
                bestStudentPosition.clear();
                maxGrade = scores[i];
                bestStudentPosition.add(new Integer(i));
            } else if (scores[i] == maxGrade) {
                bestStudentPosition.add(new Integer(i));
            }
            if (i > 0 && scores[i] < minGrade) {
                worstStudentPosition.clear();
                minGrade = scores[i];
                worstStudentPosition.add(new Integer(i));
            } else if (scores[i] == minGrade) {
                worstStudentPosition.add(new Integer(i));
            }

        }

        avg = sum / totalStudents;
        System.out.print("Highest score: ");
        for (Integer position : bestStudentPosition) {
            System.out.println(maxGrade + ", " + names[position]);
        }
        System.out.print("Lowest score: ");
        for (Integer position : worstStudentPosition) {
            System.out.println(minGrade + ", " + names[position]);
        }

        System.out.printf("Average: %3.2f", avg);

    }
}

在这些条件之前,将minGrade分配为scores[0]初始值。 例如

minGrade = scores[0];

这是输出:

你班上有多少学生? 3

姓名:哈里

得分:90.2

姓名:洛朗(Laurent)

得分:99.99

姓名:达伦

得分:98.9

最高分:99.99,洛朗

最低分:90.2,哈里

平均:96.36

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM