我的代码将名称写到XML文档,但不写任何测试分数。即使我更改名称,考试成绩也始终输出0。我会很感激找出原因的任何帮助。我已经用主方法附加了类,并用构造函数附加了类。谢谢您的帮助!
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();
}
}
}
public class Studentp194 {
//instance variable
private String name;
private int test1;
private int test2;
private int test3;
//constructor method
public Studentp194()
{
}
public Studentp194(String name, int test1, int test2, int test3)
{
this.name = name;
this.test1 = test1;
this.test2 = test2;
this.test3 = test3;
}
//Other Methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setScore(int i, int score){
if (i == 1)
test1 = score;
if(i == 2)
test2 = score;
else
test3 = score;
}
public int getScore(int i)
{
if (i == 1)
return test1;
if (i == 2)
return test2;
else
return test3;
}
public int getAverage()
{
int average = (int)Math.round((test1 + test2 + test3) / 3);
return average;
}
public int getHighScore()
{
int highScore = test1;
if(test2 > highScore)
highScore = test2;
if(test3 > highScore)
highScore = test3;
return highScore;
}
public String toString()
{
String str = "Name: " + name + "\n" +
"Test 1: " + test1 + "\n" +
"Test 2: " + test2 + "\n" +
"Test 3: " + test3 + "\n" +
"Average: " + getAverage();
return str;
}
}
技术上正确的最短答案,但对不熟悉Java Bean的人无用:Studentp194
没有score
属性。因此,score
不会将不存在的XMLEncoder
属性编码为XML。
要真正了解正在发生的事情,如果您想继续使用XMLEncoder
,对于您要解决的实际问题,它可能是最佳的解决方案,也可能不是最佳解决方案,那么您必须仔细阅读古老的文章,但是(恕我直言)棒极了Java Beans specification。
要使任意类具有名为foo
且类型为Foo
的Java Beans属性,则它必须具有public Foo getFoo()
方法和public void setFoo(Foo foo)
方法。如您所见,您的Studentp194
类没有public int getScore()
方法,也没有public void setScore(int score)
方法。
((当然,Java Beans规范实际上并未强制要求这些是方法的必需名称。您可以通过多种方式(包括BeanInfo
classes的用法)对其进行自定义,但这远远超出了范围。这个问题。)
回到您的问题和代码,由于您的BeanInfo
方法采用参数,而XMLEncoder
方法采用多个参数,因此getScore
不能按原样帮助您进行类设计。因此,它们不是Java Beans属性。因此setScore
不会对它们进行编码。
假设您出于任何原因想要继续使用XMLEncoder
。然后,您必须重构您的类以符合Java Beans规范。我将其留给读者练习。 :-)
我猜这是某种分配,使用XMLEncoder
是分配的要求。如果我错了,请查看另一种文件格式来存储您的信息并进行检索。
如果我是对的,那么您想看看您的XMLEncoder
类在逻辑上与collection或分数数组相关联的事实。您可能想要查看实际创建一个类来表示测试成绩的方向,并在Studentp194
类中存储此类测试成绩的集合。甚至更好的是,也许您的Studentp194
类希望与另一个类(如Studentp194
)或可以正确存储此信息的类似内容相关。有关此分解问题的更多方法,请阅读“第三范式”,然后从那里开始。希望对您有所帮助,并欢迎您使用StackOverflow。