我写了一个程序,它从一个包含学生姓名的文件中读取数据,并为每个学生提供无限数量的分数,我必须计算平均分数,但我似乎只能使程序适用于具体的分数。我怎么能这样做它来计算任意数量的分数的平均值。顺便说一下,程序是用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)
{
}
}
}
答案 0 :(得分:2)
此代码将根据您的需要循环多次。它将首先读取名称,然后不断读取数字,直到看到另一个名称。
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);
}
答案 1 :(得分:1)
您可以尝试:
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
答案 2 :(得分:0)
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
。