gpt4 book ai didi

Java程序分级

转载 作者:行者123 更新时间:2023-12-01 19:03:22 25 4
gpt4 key购买 nike

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

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 期中期末平均字母

分配7Test$Record@4bcc946b

分配7Test$Record@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)

最佳答案

抛开异常(exception),您实际上正在打印Record类型的对象。您需要做的是重写toString()以提供对象的适当表示。

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

我还注意到,您正在使用 System.out.println('...') 中的 nextLine() 来推进扫描程序。您可能想从代码中注释掉该部分。

关于Java程序分级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11319273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com