我的代码将名称写入 XML 文档,但不写入任何测试分数。即使我改了名字,考试成绩也总是输出0。我将非常感谢任何帮助弄清楚为什么会出现这种情况的帮助。我已附加带有 main 方法的类和带有构造函数的类。感谢您的帮助!
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Studentp194Runner {
public static void main(String[] args)
{
Studentp194 s1 = new Studentp194();
Scanner reader = new Scanner(System.in);
System.out.print("Enter student name: ");
s1.setName(reader.nextLine());
System.out.print("Enter the student's first score: ");
s1.setScore(1, reader.nextInt());
System.out.print("Enter the student's second score: ");
s1.setScore(2, reader.nextInt());
System.out.print("Enter the student's third score: ");
s1.setScore(3, reader.nextInt());
try
{
FileOutputStream fos = new FileOutputStream(new File("./student.xml"));
XMLEncoder encoder = new XMLEncoder(fos);
encoder.writeObject(s1);
encoder.close();
fos.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
try
{
FileInputStream fis = new FileInputStream(new File("./student.xml"));
XMLDecoder decoder = new XMLDecoder(fis);
Studentp194 p2 = (Studentp194)decoder.readObject();
decoder.close();
fis.close();
System.out.println("Student 1 name: " + p2.getName());
System.out.println("Test 1: " + p2.getScore(1));
System.out.println("Test 2: " + p2.getScore(2));
System.out.println("Test 3: " + p2.getScore(3));
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
catspeake
相关分类