我正在为我获得的驱动程序编写课程,但因为它正在进行,
System.println(game.toString());
我以为我需要它来归还一块板子。 我必须创建一个toString方法,它返回一个内部带有字符2d数组的板,数组中的每个字符都出现在这样的板中。 (数字2是2d数组[0] [0]的字符)。
___________
| | | |
|___|___|___|
| | | |
|___|___|___|
| | | |
|___|___|___|
| | | |
|___|___|___|
| 2 | | |
|___|___|___|
董事会代表一个建筑物,我正在制作的课程从用户那里获得输入并产生这个。在这种情况下,用户输入5个楼层和3个房间。上图表示玩家2输入0,0。这导致[0] [0]处的2d数组成为字符'2'。
这是我的编码,它没有显示我想要的内容。
public String toString(){
String Roof ="";
for( int i = 0; i < room; i++){
Roof += " __";
}
Roof += "\n";
String newLine = "\n";
String walls = "|";
for (int i = myHidingPlaces.length-1; i > 0 ; i--) {
for ( int j = 0 ; j < myHidingPlaces[0].length -1 ; j++){
if(j >= myHidingPlaces[0].length -1){
walls += "\n";
}else if(j < myHidingPlaces[0].length -1){
walls += myWinner+ " " + myHidingPlaces[i][j] + "|";
}
}
}
// "|__|__|__|\n";
return Roof + walls;//Arrays.toString(myHidingPlaces);
}
当我打2层和2个房间时 这是我的编码显示的内容:
__ __
| |
答案 0 :(得分:0)
我重写了toString方法以获得以下输出。
___________
| | | |
|___|___|___|
| | | |
|___|___|___|
| | 6 | |
|___|___|___|
| | | |
|___|___|___|
| | | |
|___|___|___|
这是您的代码,经过修改后可以运行。
package com.ggl.testing;
public class Board {
private String[][] myHidingPlaces;
// private String myWinner;
public Board() {
// this.myWinner = "";
this.myHidingPlaces = new String[3][5];
initializeMyHidingPlaces();
}
private void initializeMyHidingPlaces() {
for (int i = 0; i < myHidingPlaces.length; i++) {
for (int j = 0; j < myHidingPlaces[i].length; j++) {
myHidingPlaces[i][j] = " ";
}
}
myHidingPlaces[1][2] = "6";
}
public String toString() {
StringBuilder builder = new StringBuilder();
String newLine = System.getProperty("line.separator");
String wall = "|";
builder.append(" ");
for (int i = 0; i < myHidingPlaces.length; i++) {
if (i < (myHidingPlaces.length - 1)) {
builder.append("____");
} else {
builder.append("___ ");
}
}
builder.append(newLine);
for (int i = (myHidingPlaces[0].length - 1); i >= 0; i--) {
for (int j = 0; j < myHidingPlaces.length; j++) {
builder.append(wall);
builder.append(" ");
builder.append(myHidingPlaces[j][i]);
builder.append(" ");
}
builder.append(wall);
builder.append(newLine);
for (int j = 0; j < myHidingPlaces.length; j++) {
builder.append(wall);
builder.append("___");
}
builder.append(wall);
builder.append(newLine);
}
return builder.toString();
}
public static void main(String[] args) {
Board board = new Board();
System.out.println(board);
}
}