2014-03-06 49 views
1

我写了一个程序,它从一个文件中读取数据,该文件包含学生的名字和每个学生不定数量的分数,我必须计算平均分数,但我似乎只能使程序为特定数量的分数工作。我怎么能做到这一点,因此它可以计算任何数量的平均分数。顺便说一下,该程序是用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

是否得分的数量每名学生不同,或者是它不变? –

回答

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来避免您可以使用doublefloat的精度问题。

输入:

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 
+0

出于所有类型的建议,这是对我最有帮助的一个,谢谢,我没有想到的是使用第二个循环 – FpRoT

0

``两个或两个以上,使用了',Dijkstra算法说:

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

+0

这也只能工作10年级,但不是更多(或更少)比10 ... – exception1

+0

解决我希望,谢谢你的抬头... –

+0

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 – FpRoT

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); 
}