Java 如何阅读ArrayList<&燃气轮机;到ArrayList<&燃气轮机;把它打印到终端上?
我想做一个垄断型的游戏,这是我在这个问题上提出的第二个问题。我有一个棋盘班和一个正方形班。在我的主类中,我使用Board类中名为Java 如何阅读ArrayList<&燃气轮机;到ArrayList<&燃气轮机;把它打印到终端上?,java,arraylist,Java,Arraylist,我想做一个垄断型的游戏,这是我在这个问题上提出的第二个问题。我有一个棋盘班和一个正方形班。在我的主类中,我使用Board类中名为addSquare的方法创建正方形,然后该方法将有关某个正方形的信息存储在square类中。之后,我试图在getSquare方法中读取Board类中的某个正方形,但我不知道如何执行该操作 我提供的测试解决方案仅用于测试电路板力学: 主要类别: public class Main { public static void main(String[] args)
addSquare
的方法创建正方形,然后该方法将有关某个正方形的信息存储在square类中。之后,我试图在getSquare
方法中读取Board类中的某个正方形,但我不知道如何执行该操作
我提供的测试解决方案仅用于测试电路板力学:
主要类别:
public class Main {
public static void main(String[] args) {
Board board = new Board();
Action act = new Action();
board.addSquare("0,Country in conflict.", act);
board.getSquare(0);
}
}
董事会级别:
public class Board {
public ArrayList<Square> Board;
public Board(){
this.Board = new ArrayList<Square>();
}
public void addSquare(String square, Action action){
Square sq = new Square(square, action);
Board.add(sq);
}
public void addSquare(String square, Action action1, Action action2){
Square sq = new Square(square, action1, action2);
Board.add(sq);
}
public void getSquare(int square){
Board.get(square);
}
}
公共课程委员会{
公共ArrayList委员会;
公共委员会(){
this.Board=new ArrayList();
}
公共void addSquare(字符串方形,动作){
Square sq=新的Square(Square,action);
加上(平方米);
}
公共void addSquare(字符串square、操作action1、操作action2){
Square sq=新的Square(Square,action1,action2);
加上(平方米);
}
公共广场(内部广场){
板。得到(正方形);
}
}
方形类:
public class Square{
public ArrayList<Action> actions;
public final String text;
public final int squareNumber;
public Square(String textGiven, Action action){
String textArray[] = textGiven.split(",");
this.squareNumber = Integer.parseInt(textArray[0]);
this.text = textArray[1];
this.actions = new ArrayList<Action>();
actions.add(action);
}
public Square(String textGiven, Action action1, Action action2){
String textArray[] = textGiven.split(",");
this.squareNumber = Integer.parseInt(textArray[0]);
this.text = textArray[1];
this.actions = new ArrayList<Action>();
actions.add(action1);
actions.add(action2);
}
public Action getAction(Action action){
return action;
}
public int getNumber(){
return squareNumber;
}
public String getText(){
return text;
}
public String toSrting(){
return squareNumber + ". " + text;
}
}
公共类广场{
公共诉讼;
公共最终字符串文本;
公共最终整数平方数;
公共方块(字符串文本给定,动作){
字符串textArray[]=textGiven.split(“,”);
this.squareNumber=Integer.parseInt(textArray[0]);
this.text=textArray[1];
this.actions=new ArrayList();
动作。添加(动作);
}
公共广场(字符串文本给定,动作action1,动作action2){
字符串textArray[]=textGiven.split(“,”);
this.squareNumber=Integer.parseInt(textArray[0]);
this.text=textArray[1];
this.actions=new ArrayList();
增加(措施1);
增加(行动2);
}
公共行动getAction(行动){
返回动作;
}
public int getNumber(){
返回平方数;
}
公共字符串getText(){
返回文本;
}
公共字符串toSrting(){
返回平方数+“+”文本;
}
}
注意:只要是说行动是一个单独的行动类的球员需要做。我拥有正确的类,因此我不提供信息,但如果需要,我将使用Action类的代码进行更新。
get
此处返回一个正方形,但您根本没有使用它:
public void getSquare(int square){
Board.get(square);
}
与所有其他getXXX
方法一样,getSquare
应该返回一个值:
public Square getSquare(int square){
return Board.get(square);
}
您可以通过System.out.println
打印返回值,您可能已经知道:
System.out.println(board.getSquare(0));
我还注意到您将拼错为字符串
:
public String toSrting(){ // <----
return squareNumber + ". " + text;
}
publicstringtosrting(){//我已经试过了,它会打印出这个消息:board.square.test。Square@129a8472@卡莫拉,你在Square
中拼错了toString
。哦,天啊,是的,你是对的……我为这个项目花了好几个小时,那些愚蠢的错误发生在你头脑发狂,不专注于简单的事情的时候……我刚刚做到了!再次感谢你的帮助你的帮助!