Java程序评分

时间:2012-07-03 21:13:02

标签: java

我已经在这个程序上工作了几个小时,我无法弄清楚如何让程序从分数文本文件中实际打印成绩

public class Assign7{
  private double finalScore;
  private double private_quiz1;
  private double private_quiz2;
  private double private_midTerm;
  private double private_final;
  private final char grade;

  public Assign7(double finalScore){
    private_quiz1 = 1.25;
    private_quiz2 = 1.25;
    private_midTerm = 0.25;
    private_final = 0.50;

       if (finalScore >= 90) {
           grade = 'A';
       } else if (finalScore >= 80) {
           grade = 'B';
       } else if (finalScore >= 70) {
           grade = 'C';
       } else if (finalScore>= 60) {
           grade = 'D';
       } else {
           grade = 'F';
       }
}


  public String toString(){
    return finalScore+":"+private_quiz1+":"+private_quiz2+":"+private_midTerm+":"+private_final;

  }
} 

此代码编译以及此代码

import java.util.*;
import java.io.*;

public class Assign7Test{
  public static void main(String[] args)throws Exception{

  int q1,q2;
  int m = 0;
  int f = 0;
  int Record ; 
  String name;

    Scanner myIn = new Scanner( new File("scores.txt") );

    System.out.println( myIn.nextLine() +"  avg  "+"letter");

   while( myIn.hasNext() ){
      name = myIn.next();
      q1 = myIn.nextInt();
      q2 = myIn.nextInt();
      m = myIn.nextInt();
      f = myIn.nextInt();
       Record myR = new Record( name, q1,q2,m,f);
       System.out.println(myR);

      }
    }

public static class Record {

       public Record() {
       }

       public Record(String name, int q1, int q2, int m, int f)
       { 

       } 
   } 
}

一旦编译了代码,我就得到了这个代码,它精确地计算了我在scores.txt中的数字 名称quiz1 quiz2 midterm final avg letter

Assign7Test $记录@ 4bcc946b

Assign7Test $记录@ 642423

Exception in thread "main" java.until.InputMismatchException    
    at java.until.Scanner.throwFor(Unknown Source)    
    at java.until.Scanner.next(Unknown Source)    
    at java.until.Scanner.nextInt(Unknown Source)        
    at java.until.Scanner.nextInt(Unknown Source)    
    at Assign7Test.main(Assign7Test.java:25)

3 个答案:

答案 0 :(得分:4)

除了例外,您实际上 打印Record类型的对象。您需要做的是覆盖toString()以提供对象的正确表示。

@Override
public String toString() {
    return "Something meaningful about your Record object.";
}

我还注意到您使用nextLine()中的System.out.println('...')推进了扫描程序。您可能希望从代码中评论该部分。

答案 1 :(得分:0)

您收到此错误的原因是因为您需要一个整数,但扫描程序读取的下一个内容不是数字。

另外,将它放在记录的toString中以停止打印地址。

public static class Record {

   public Record() {
   }

   public Record(String name, int q1, int q2, int m, int f)
   { 

   }
    public String toString(){}//print out stuff here.

}

答案 2 :(得分:0)

将您的记录更改为此类

public static class Record {  
    String name;
    int q1;
    int q2;
    int m;
    int f;

    public Record() {}         
    public Record(String name, int q1, int q2, int m, int f) {
        // here you save the given arguments localy in the Record.
        this.name = name;
        this.q1 = q1;
        this.q2 = q2;
        this.m = m;
        this.f = f;
    }
@Override
public String toString(){
   //here you write out the localy saves variables.
   //this function is called when you write System.out.println(myRecordInstance);
   System.out.println(name + ":" + q1 + ":" + q2 + ":" + m + ":" + f);
}

}

它的作用:你必须通过创建记录来保存这些记录。 另外,如果要使用System.out.println(myRecordInstance),则必须覆盖toString方法;相反,您可以编写另一个函数在Record中返回一个String并打印出此函数的返回值,如System.out.println(myRecordInstace.writeMe());然后您将该函数添加到记录中。

public String writeMe(){
System.out.println(name + ":" + q1 + ":" + q2 + ":" + m + ":" + f);
}
相关问题