Java 平均分数简化

Java 平均分数简化,java,Java,我写了一个程序,从一个文件中读取数据,该文件包含一个学生的名字和每个学生的不确定分数,我必须计算平均分数,但我似乎只能让程序在特定分数下工作。我怎样才能使它计算出任意数量分数的平均值呢。顺便说一下,这个程序是用Java编写的 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package hw1; import java.io.*; im

我写了一个程序,从一个文件中读取数据,该文件包含一个学生的名字和每个学生的不确定分数,我必须计算平均分数,但我似乎只能让程序在特定分数下工作。我怎样才能使它计算出任意数量分数的平均值呢。顺便说一下,这个程序是用Java编写的

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hw1;
import java.io.*;
import java.util.Scanner;

/**
 *
 * @author admin
 */
public class HW1 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    try
    {
    File file = new File("/Users/admin/Desktop/studentScores.in");  //reads form file

    Scanner scan = new Scanner(file); //scans the file

    while(scan.hasNextInt()) //while the scanner identifies integers
    {
         String name = scan.next();

         String grade1 = scan.next();

         String grade2 = scan.next();

         String grade3 = scan.next(); 

         String grade4 = scan.next(); 

         String grade5 = scan.next(); 

         String grade6 = scan.next(); 

         String grade7 = scan.next(); 

         String grade8 = scan.next(); 

         String grade9 = scan.next(); 

         String grade10 = scan.next(); 

         int average = (Integer.parseInt(grade1) + Integer.parseInt(grade2) + Integer.parseInt(grade3) + Integer.parseInt(grade4) + Integer.parseInt(grade5) + Integer.parseInt(grade6) +Integer.parseInt(grade7) + Integer.parseInt(grade8) +              Integer.parseInt(grade9) + Integer.parseInt(grade10)) / 10;
         //decalre avergae variable and calculate it
         System.out.println("Name:" + name + " Average:" + average); // print the name of the        student and their average
    }
    }

    catch(IOException e)
    {

    }
}
}
``Dijkstra说,两个或两个以上,用a表示“”:

int numberOfScoresPerStudent = 10;

while(scan.hasNext()) {
     String name = scan.next();
     int sum = 0x00;
     while(scan.hasNextInt()) {
        sum += scan.nextInt();
     }
     int average = sum/ numberOfScoresPerStudent;
     System.out.println("Name:" + name + " Average:" + average);
}
此外,建议将平均值的计算修改为:

double average = sum/ (double) numberOfScoresPerStudent;

否则,
6+5
将平均产生
5

此代码将根据需要循环多次。它将首先读取中的名称,然后不断读取数字,直到看到另一个名称

String next = scan.next();
while(scan.hasNext()) {
    String name = next;
    next = scan.next();
    int total = 0;
    int count = 1;
    while(!next.matches("^[a-zA-Z]*$")) {
        total += Integer.parseInt(next)
        count++
        next = scan.next();
    }
    int average = total/count;
    System.out.println("Name: "+name+" Average: "+average);
}
你可以试试:

public static void main(String[] args) {
    final File file = new File("/Users/admin/Desktop/studentScores.in");  // reads from file

    try {
        final Scanner  scan = new Scanner(file); // scans the file


        while (scan.hasNext()) { // while the scanner identifies student names
            final String name = scan.next();

            int nbGrades = 0;
            int totalGrades = 0;

            while (scan.hasNextInt()) { //while the scanner identifies grades
                int grade = scan.nextInt();

                nbGrades++;
                totalGrades += grade;
            }

            // declare average variable and calculate it
            final BigDecimal average = new BigDecimal(totalGrades).divide(new BigDecimal(nbGrades));

            System.out.println("Name:" + name + " Average:" + average);
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
其思想是使用两个循环:

  • 一个可以得到学生的名字
  • 一个人去拿分数
在第二个循环中,通过增加
nbGrades
,计算找到的成绩数,并根据找到的成绩增加成绩总数

对于平均值,它使用了一个
BigDecimal
,以避免使用
double
float
时可能出现的精度问题

输入:

Agnes 56 82 95 100 68 52 Bufford 87 92 97 100 96 85 93 77 98 86 Julie 99 100 100 89 96 100 92 99 68 Alice 40 36 85 16 0 22 72 Bobby 100 98 92 86 88
输出:

Name:Agnes Average:76
Name:Bufford Average:91
Name:Julie Average:94
Name:Alice Average:39
Name:Bobby Average:93

10分与此无关,它只是给一个学生的分数最多,其他学生的分数较少。这是名单。Agnes 56 82 95 100 68 52 Bufford 87 92 97 100 96 85 93 77 98 86 Julie 99 100 100 89 96 100 92 99 68 Alice 40 36 85 16 0 22 72 Bobby 100 98 92 86 88您可以相应地设置
numberOfScoresPerStudent
参数。@FpRoT您应该用该数据更新问题,以便其他人可以提供相关答案。您的每个学生的分数各不相同,还是不变?在所有的建议中,这是对我帮助最大的一个,谢谢,我没有想到的是使用第二个循环