簡體   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