我是否可以知道如何序列化和反序列化此程序,我无法弄明白,我是否应该使用数组来存储正在打印的内容..?
它有两个单独的calss文件中的两个类。 一个是玩家,另一个是SnakeAndladder1
Player class
------------
package coll.org;
public class Player {
private String name;
private int score;
Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
SnakeAndladder1 class
---------------------
package coll.org;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;
import java.util.Scanner;
public class SnakeAndladder1 {
ArrayList<String> name1=new ArrayList<String>(); //an array to store players names
public int throwdice() //to calculate the dice value
{
return (int)(Math.random()*6)+1;
}
public int ladder(int curscore)
{
Hashtable<Integer,Integer> ld = new Hashtable<Integer,Integer>();
//Map<Integer,Integer> ldmap = new Map<Integer,Integer>();
ld.put(15,30);
ld.put(45,71);
ld.put(25,62);
ld.put(81,91);
ld.put(9,39);
while(curscore!=15 || curscore!=45 || curscore!=25 || curscore!=81 || curscore!=9)
{ return curscore;}
int v=ld.get(15);
return v;
}
public int snake(int curscore)
{
Hashtable<Integer,Integer> ld = new Hashtable<Integer,Integer>();
ld.put(29,11);
ld.put(81,48);
ld.put(30,6);
ld.put(92,71);
ld.put(58,19);
while(curscore!=29 || curscore!=81 || curscore!=30 || curscore!=92 || curscore!=58 )
{return curscore;}
int v=ld.get(curscore);
return v;
}
public boolean Game (Player p){
//int score=0;
//String name;
int v=0;
//name=name1.toString();
System.out.println("Click y to roll dice");
Scanner in2=new Scanner(System.in);
String yes=in2.nextLine();
if(yes.equalsIgnoreCase("y"))
{
v=throwdice();
System.out.println("dice value:"+v);
}
p.setScore(p.getScore()+v);
if(p.getScore()==100)
{
System.out.println("User:"+p.getName()+"got"+v+".Winner!!!");
return false;
}
if (p.getScore()>100)
{
p.setScore(p.getScore()-v);
System.out.println("Current score of"+p.getName()+"is"+p.getScore());
return true;
}
int s1=ladder(p.getScore());
if(s1==p.getScore())
{
p.setScore(snake(p.getScore()));
System.out.println("Current score of"+p.getName()+"is"+p.getScore());
return true;
}
else
{
p.setScore(s1);
System.out.println("Current score of"+p.getName()+"is"+p.getScore());
return true;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int l=0;
//boolean flag=true;
System.out.println("Enter the number of players:");
Scanner in=new Scanner(System.in);
int n=in.nextInt();
System.out.println("Enter Players names in order:");
ArrayList<Player> p1=new ArrayList<Player>(); //an array to store players names
for (int i=0;i<n;i++)
{
Scanner in1=new Scanner(System.in);
String name2=in1.nextLine();
Player p = new Player(name2);
p1.add(p);
//name1.add(name2);
}
//Snakeandladder1[] players = new Snakeandladder1[n];
//ArrayList<Snakeandladder1> player=new ArrayList<Snakeandladder1>();
//for(int i=0;i<n;i++)
//player.add(new Snakeandladder1());
// SnakeAndladder1 players[];
// players = new SnakeAndladder1[n];
SnakeAndladder1 sk = new SnakeAndladder1();
while(true)
{
for (int i=0;i<n;i++)
{
Player p3=p1.get(i);
boolean flag = sk.Game(p3);
if (flag == false)
{System.exit(0);}
}
}
}
}
这个程序允许任意数量的玩家并要求用户输入y来掷骰子,我有输出但我无法序列化它。有没有办法在程序执行完成后获取输出并将其序列化?
答案 0 :(得分:0)
存在一个整齐的库,用于将对象序列化为XML文件(当然,之后将其反序列化)。它非常简单易用且非常有效。