我在序列化方面遇到了问题,我需要用java实现它
我已经创建了一个程序,这是一个猜测游戏,但我在学习这个新方法序列化它时遇到了问题。我已经做了一切,我只需要使它,使程序可以保存和加载通过序列化方法打开。我正在尝试序列化游戏部分或程序的播放方法,以便下次加载时加载旧信息我在序列化方面遇到了问题,我需要用java实现它,java,serialization,Java,Serialization,我已经创建了一个程序,这是一个猜测游戏,但我在学习这个新方法序列化它时遇到了问题。我已经做了一切,我只需要使它,使程序可以保存和加载通过序列化方法打开。我正在尝试序列化游戏部分或程序的播放方法,以便下次加载时加载旧信息 import javax.swing.JOptionPane; import java.io.Serializable; import java.io.ObjectOutputStream; import java.io.FileOutputStream; import
import javax.swing.JOptionPane;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//Node class
class Node
{
//instance variables
public String questionText;
public Node leftChild;
public Node rightChild;
public void displayText()
{
System.out.println(questionText);
}
}
//Tree class
class Tree implements Serializable
{
private Node root;
//constructor
public Tree()
{ root = new Node();
root.leftChild = new Node();
root.rightChild = new Node();
root.questionText = "Does it live on land?";
root.leftChild.questionText ="bear"; // left side is Yes, right side is No
root.rightChild.questionText = "parrot";
}
public void instruction()
{
JOptionPane.showMessageDialog(null, "Think of an animal, I will try to guess it, answer yes or no");
}
public void play()
{
Node current = root;
Node parent = current;
boolean isLeftChild = true;
while(true)
{ parent = current;
int response = JOptionPane.showConfirmDialog(null,current.questionText );
//code here for yes
if (response == JOptionPane.YES_OPTION)
{
current = current.leftChild;
isLeftChild=true;
}
//code here for no
else if (response == JOptionPane.NO_OPTION)
{
current = current.rightChild;
isLeftChild = false;
}
if (current.leftChild == null && current.rightChild == null)
{
int secondQ = JOptionPane.showConfirmDialog(null, "Is your animal a " + current.questionText + "?");
if (secondQ == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null,"I Guessed your animal!");
return;
}
else if (secondQ == JOptionPane.NO_OPTION)
{
Node nodeOne = new Node();
Node nodeTwo = new Node();
nodeOne.questionText = JOptionPane.showInputDialog("Write a question that differentiates your animal from the animal I guessed, it would be yes for your animal");
nodeTwo.questionText = JOptionPane.showInputDialog("What is this animal?");
nodeOne.rightChild = current;
nodeOne.leftChild = nodeTwo;
// parent.leftChild = nodeOne or parent.rightChild = nodeOne
if(isLeftChild == false)
{
parent.rightChild = nodeOne;
System.out.println("right child");
}
else
{
parent.leftChild = nodeOne;
System.out.println("left Child");
}
return;
}
}
}
}
public void preOrder(Node localRoot)
{
if(localRoot != null)
{
System.out.print(localRoot.questionText + " ");
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
}
public Node getRoot(){
return root;
}
}
public class GuessTheAnimal
{
public static void main(String[] args)
{
Tree animal = new Tree();
animal.instruction();
animal.play();
animal.play();
}
}
我建议:
阅读同样容易:
FileInputStream fileIn = new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
tree = (Tree) in.readObject();
in.close();
我建议:
阅读同样容易:
FileInputStream fileIn = new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
tree = (Tree) in.readObject();
in.close();
我发现代码有两个问题 首先,树实现了可序列化,但这还不够。您需要在程序启动后的早期告诉程序,以读取序列化文件,并从该输入流初始化树。相反,当您准备在程序退出之前保存状态时,您需要将树写入序列化文件 其次,树实现了可序列化,但树是由节点对象构建的,而节点对象不实现可序列化。序列化类的所有成员自身都必须是可序列化的,才能使序列化工作
关于的一个很好的教程,我发现代码有两个问题 首先,树实现了可序列化,但这还不够。您需要在程序启动后的早期告诉程序,以读取序列化文件,并从该输入流初始化树。相反,当您准备在程序退出之前保存状态时,您需要将树写入序列化文件 其次,树实现了可序列化,但树是由节点对象构建的,而节点对象不实现可序列化。序列化类的所有成员自身都必须是可序列化的,才能使序列化工作
关于你的问题相当广泛,这是一个很好的教程。 首先,您应该让树类实现java.io.Serializable: 类树实现了可序列化{ ... } 然后,确保所有字段树以相同的方式实现可序列化,以此类推 完成此操作后,可以使用ObjectOutputStream写出树实例,并使用ObjectInputStream读取它 例如,new ObjectOutputStreamnew FileInputStreamfilename.writeObjecttree
但我建议,您需要先了解一下Serializable和ObjectOutputSteam。您的问题相当广泛。 首先,您应该让树类实现java.io.Serializable: 类树实现了可序列化{ ... } 然后,确保所有字段树以相同的方式实现可序列化,以此类推 完成此操作后,可以使用ObjectOutputStream写出树实例,并使用ObjectInputStream读取它 例如,new ObjectOutputStreamnew FileInputStreamfilename.writeObjecttree
但我建议,您需要先了解一下Serializable和ObjectOutputSteam。+1无需使用fileIn.close;然后退出。关闭;,自年起,关闭;离得很近;将为您关闭它们有什么地方可以放这个吗?在Tree类中,之前和之后?在主方法中,IMO。如果没有关于您尝试做什么的更坚实的要求,很难判断。您不会在Tree类中这样做。然而,这确实表明了设计中的另一个问题——您应该将程序逻辑与数据树分离。您的猜测TheAnimal类应该具有关于如何序列化/反序列化树、如何遍历树以及询问问题的所有逻辑,并且您的树类应该只包含问题/动物对的节点。这本身可能有助于澄清有关序列化的一些问题,实际序列化的内容以及应该发生的位置。我只是尝试将从用户收集的信息保存在play方法中,以便下次运行程序时,所有旧答案仍然存在+1不需要fileIn.close;然后退出。关闭;,自年起,关闭;离得很近;将为您关闭它们有什么地方可以放这个吗?在Tree类中,之前和之后?在主方法中,IMO。如果没有关于您尝试做什么的更坚实的要求,很难判断。您不会在Tree类中这样做。然而,这确实表明了设计中的另一个问题——您应该将程序逻辑与数据树分离。您的猜测TheAnimal类应该具有关于如何序列化/反序列化树、如何遍历树以及询问问题的所有逻辑,并且您的树类应该只包含问题/动物对的节点。这本身可能有助于澄清有关序列化的一些问题,实际序列化的内容以及应该发生在哪里
为了将从用户那里收集的信息保存在play方法中,以便下次运行程序时,所有的旧答案仍然存在。我不知道如何实现它,需要帮助。我不知道如何实现它,需要帮助。是的,我真的不知道从何处开始,我将查看指南,我读过一些其他的但不完全理解,这就是为什么我在这里发布。是的,我真的不知道从哪里开始,我会看一下指南,我读过一些其他的但不完全理解,这就是为什么我在这里发布。