0

我已经在这个程序上工作了几个小时,但我不知道如何让程序从分数文本文件中实际打印成绩

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)
       { 

       } 
   } 
}

一旦编译了代码,我就会得到这个,它会准确计算我在 score.txt 中的数字 名称 quiz1 quiz2 期中最终平均字母

Assign7Test$Record@4bcc946b

分配7测试$记录@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)
4

3 回答 3

4

Exception aside, you actually are printing objects of type Record. What you would need to do is override toString() to provide a decent representation of your object.

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

I also note that you're advancing the Scanner by use of nextLine() in System.out.println('...'). You may want to comment that part out of your code.

于 2012-07-03T21:16:13.700 回答
0

您收到此错误的原因是因为您需要一个整数,但您的扫描仪接下来读取的不是数字。

此外,将其放入记录的 toString 中以停止打印地址。

IE

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.

}

于 2012-07-03T21:17:57.453 回答
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);
}
于 2012-07-10T15:08:24.223 回答