读取带有逗号和空格的文本文件以计算 GPA



我需要使用缓冲读取器和文件读取器,并在这样的列下格式化它。

System.out.println("    Name               GPA  Classes        Hours/Grades" 
System.out.println("    ------------------ ---- -------- ------------------------ ");

以下是我想计算 GPA 的方式。将字母等级的分值乘以学分小时数。结果是获得的质量积分。 该学期的总学分;合计学期的质量点数。 将总质量点除以总学分

以下是文本文件中的信息:

Anagram,Anna,3/A,4/A,3/A,3/A,3/A
Boop,Betty,3/B,3/B,4/B,3/B,3/B,1/B
Chitwilly,Cindy,4/C,3/C,3/C,3/C,3/C
Downer,Debbie,3/D,4/D,4/D,3/D,1/D
Everhappy,Eva,4/B,3/B,3/D
Froghat,Freda,3/F,4/F,4/F,3/F
Getterdone,Gina,4/B,4/B,3/B,3/B,3/C
Hotentott,Heidi,3/D,4/D,4/C,3/C,1/B
Imagooden,Inga,4/B,4/B
Jitterbug,Jeannie,3/B,3/C,4/C,3/F,3/C,1/C
Keeper,Kendra,3/A,3/B,3/C
Lovermore,Lynda,4/A,4/B,4/C,4/D
Moocher,Minnie,3/B,3/C,3/D
Nussbaum,Nancy,4/C,4/D,4/F
Oglethorpe,Olga,3/B
Poppledooper,Penny,3/D,3/F
Qutie,Quinn,2/A,4/C,2/F

这是我的第一次尝试。我是新手,但我真的很想理解这一点,因为我的教授只是没有很好地解释它,无法理解。

import java.io.*;
import java.util.StringTokenizer;
    public class p02gpa {
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
                String         file = "p02-grades.txt";
                BufferedReader br   = new BufferedReader(new      FileReader(file));
                String          rec;
                String          fmt = "%2d: %-12s %3d %-8s %-20 %n";
                System.out.println("    Name               GPA  Classes        Hours/Grades");
                System.out.println("    ------------------ ---- -------- ------------------------ ");
                while((rec=br.readLine())!= null)
                    calcDisp(rec);
                br.close();
                  }
                  //----------------------------------------------------------------------------
                  public static void calcDisp(String rec)
                  {
                        StringTokenizer tok;
                        String          name;
                        String          fname;
                        String          lname;
                        double          ptValue;
                        double          classes;
                        double          qualtyPts;
                        double          hours;
                        double          gpa;
                        int             ctr=0;
                        tok     = new StringTokenizer(rec,",|/|");
                        lname   = tok.nextToken();
                        fname   = tok.nextToken();
                        name    = fname+ " "+lname;
                        System.out.println(name);
                        ctr++;
                        while(tok.hasMoreTokens())
                        {
                            String nameS =tok.nextToken();
                            String letterGrade =tok.nextToken();
                            System.out.println(letterGrade);
                            String hrsStr = tok.nextToken();
                            hours =Integer.parseInt(hrsStr);
                            System.out.println(hours);

                            if(letterGrade == "A")
                                ptValue = 4.0;
                            if(letterGrade == "B")
                                ptValue = 3.0;
                            if(letterGrade == "C")
                                ptValue = 2.0;
                            if(letterGrade == "D")
                                ptValue = 1.0;
                            if(letterGrade == "F")
                                ptValue = 0.0;

                              qualtyPts = ptValue * hours;
                              gpa = qualtyPts / hours;
import java.io.*;
import java.util.StringTokenizer;
public class p02gpa
{
public static void main(String[] args) throws IOException 
{
String  file = "p02-grades.txt";
BufferedReader br   = new BufferedReader(new      FileReader(file));
String          rec;
String          fmt = "%2d: %-12s %3d %-8s %-20 %n";
System.out.println("    Name               GPA  Classes        Hours/Grades");
System.out.println("    ------------------ ---- -------- ------------------------ ");
while((rec=br.readLine())!= null)
calcDisp(rec);
br.close();
}
public static void calcDisp(String rec)
{
StringTokenizer tok;
String          name;
String          fname;
 String          lname;
 double          ptValue=0;
double          classes;
double          qualtyPts;
 double          hours;
double          gpa;
int             ctr=0;
tok     = new StringTokenizer(rec,",|/|");
lname   = tok.nextToken();
fname   = tok.nextToken();
name    = fname+ " "+lname;
System.out.print(name);
ctr++;
while(tok.hasMoreTokens())
{
 String nameS =tok.nextToken();
String letterGrade =tok.nextToken();
System.out.print(letterGrade);
hours =Integer.parseInt(nameS);
System.out.print(hours);
if(letterGrade == "A")
ptValue = 4.0;
if(letterGrade == "B")
ptValue = 3.0;
if(letterGrade == "C")
ptValue = 2.0;
if(letterGrade == "D")
 ptValue = 1.0;
if(letterGrade == "F")
ptValue = 0.0;
qualtyPts = ptValue * hours;
gpa = qualtyPts / hours;
}
System.out.println("");
}
}

相关内容

  • 没有找到相关文章

最新更新