我的代码将名称写入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();
}
}}
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;
}}
发布于 2019-11-03 03:30:57
技术上正确但对不熟悉Java的人来说,最短的可能答案是:Studentp194没有score属性。因此,不存在的score属性不被XMLEncoder编码为XML。
要真正理解正在发生的事情,如果您想继续使用XMLEncoder (这可能是解决您想要解决的实际问题的最佳解决方案,也可能不是最佳解决方案),那么您必须阅读一下令人震惊的古老但(IMHO)非常棒的Java规范。
如果任意类都有一个名为foo的Java属性(其类型为Foo ),那么它必须有一个public Foo getFoo()方法和一个public void setFoo(Foo foo)方法。如您所见,您的Studentp194类没有public int getScore()方法,也没有public void setScore(int score)方法。
(当然,Java规范实际上并不要求这些是所需的方法名称。您可以通过多种方式定制它们,包括类的使用,但这远远超出了这个问题的范围。)
回到您的问题和代码,XMLEncoder不会在这里帮助您进行类设计,因为您的getScore方法接受参数,而您的setScore方法需要多个参数。因此,它们不是Java属性;因此,XMLEncoder不会对它们进行编码。
假设您希望以任何理由继续使用XMLEncoder。然后,您必须重构您的类以符合Java规范。我会把它留给读者做练习。:-)
我猜这是一种赋值,使用XMLEncoder是分配的一个要求。如果我错了,那么查看不同的文件格式来存储和检索您的信息。
如果我是对的,那么您想看看Studentp194类在逻辑上与集合或分数数组相关联这一事实。您可能希望实际创建一个类来表示测试分数,并将此类测试分数的集合存储在您的Studentp194类中。更好的是,您的Studentp194类可能希望与另一个类(如Transcript )相关,或者类似的类可以正确地存储这些信息。要获得更多关于分解方法来分解问题的信息,请阅读“第三范式”,然后从那里开始。我希望这有助于并欢迎来到StackOverflow。
https://stackoverflow.com/questions/58675471
复制相似问题