提问人:ICIS 提问时间:5/12/2021 最后编辑:Federico klez CullocaICIS 更新时间:5/13/2021 访问量:61
制作游戏板:你如何缩短这个创建数组的代码,你如何将它们打印出来?
MAKING GAME BOARD: How do you shorten this code of creating array, and how do you print them out?
问:
我正在开发一个游戏板,可以打印出这样的东西:
这是我写的代码
void showBoard() {
String city1, city2, city3, city4, city5, city6, city7, city8, city9, city10;
// I wrote like this because I want each box to have the size to hold 11 characters.
city1 = "Cario ";
city2 = "Phuket ";
city3 = "New delhi ";
city4 = "Hanoi ";
city5 = "Paris ";
city6 = "LA ";
city7 = "Sydney ";
city8 = "Tokyo ";
city9 = "Seoul ";
city10 = "START ";
char[][] cities = new char[10][];
for (int i = 0; i < 10; i++) {
cities[i] = new char[11];}
cities[0] = city1.toCharArray();
cities[1] = city2.toCharArray();
cities[2] = city3.toCharArray();
cities[3] = city4.toCharArray();
cities[4] = city5.toCharArray();
cities[5] = city6.toCharArray();
cities[6] = city7.toCharArray();
cities[7] = city8.toCharArray();
cities[8] = city9.toCharArray();
cities[9] = city10.toCharArray();
for (int i = 0; i < 11; i++)
System.out.print(cities[0][i]);
for(int k = 0; k < 2; k++) {
for (int q = 0; q < 5; q++) {
for(int i = 0; i < 13; i++) {
System.out.print("-");}}
System.out.println();
//There's the <Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 > error because of the code from here....
for(int i = 0; i < 2 ; i++) {
for(int j = 0; j < 5; j++) {
for (int q = 0; q < cities.length; q++) {
if (q%11 == 0)
System.out.print("|");
else {
for (int p = 0; p < cities[q].length; p++ )
System.out.print(cities[p][q]);
}
}
System.out.print(" ");
}
System.out.println();}
//to here
for (int q = 0; q < 5; q++) {
for(int i = 0; i < 13; i++) {
System.out.print("-");}}
System.out.println();
if (k == 0) {
for(int i = 0; i < 65; i++) {
if (i == 0)
System.out.print("↑");
else if (i == 64)
System.out.println("↓");
else
System.out.print(" ");}}
}
System.out.println();
}
有两个问题我真的解决不了:
1.缩短cities的代码[0~9] = city1~10.toCharArray();
2.打印出像图像一样的数组。
不能打印漂亮的独立盒子,所以我只是把盒子做成它们在倒下的梯子里
答:
0赞
Gilbert Le Blanc
5/13/2021
#1
以下是我的一次测试运行的结果。
-----------------------------------------------------------------------
| Cario | Phuket | New Delhi | Hanoi | Paris |
| | | | | |
-----------------------------------------------------------------------
-----------------------------------------------------------------------
| Los Angeles | Sydney | Tokyo | Seoul | START |
| | | | | |
-----------------------------------------------------------------------
我写了一种方法,让我说出名字最长的城市的长度。我测试了这种方法。
然后,我编写了一个创建虚线的方法。我测试了这种方法。
然后我写了一种方法,给我 10 个城市名称中的 5 个。我多次测试了这种方法。我修复了以前方法中出现的问题。
然后,我将所有方法放在一起,以创建您看到的输出。
写一点,测试很多。你不可能像这样编写一个完整的方法,并期望它正常工作。写一点,测试很多。并使用方法来分隔代码的逻辑块。showBoard
我懒得去查箭头符号。我把那段代码留给你。
下面是完整的可运行代码。
public class ConsoleGameBoard {
public static void main(String[] args) {
System.out.println(new ConsoleGameBoard().showBoard());
}
public String showBoard() {
String[] cities = { "Cario", "Phuket", "New Delhi", "Hanoi", "Paris",
"Los Angeles", "Sydney", "Tokyo", "Seoul", "START" };
StringBuilder builder = new StringBuilder();
int maxLength = getMaximumLength(cities);
builder.append(createDashedLine(cities, maxLength));
builder.append(createCityNameLine(cities, 0, maxLength));
builder.append(createBlankNameLine(maxLength));
builder.append(createDashedLine(cities, maxLength));
builder.append(System.lineSeparator());
builder.append(createDashedLine(cities, maxLength));
builder.append(createCityNameLine(cities, 5, maxLength));
builder.append(createBlankNameLine(maxLength));
builder.append(createDashedLine(cities, maxLength));
return builder.toString();
}
private StringBuilder createDashedLine(String[] cities, int maxLength) {
StringBuilder builder = new StringBuilder();
int dashLength = (maxLength + 1) * cities.length / 2 + 2 * cities.length / 2 + 1;
for (int index = 0; index < dashLength; index++) {
builder.append('-');
}
builder.append(System.lineSeparator());
return builder;
}
private StringBuilder createCityNameLine(String[] cities, int startIndex, int maxLength) {
String formatter = "%-" + maxLength + "s";
StringBuilder builder = new StringBuilder();
for (int index = startIndex; index < startIndex + 5; index++) {
builder.append("| ");
builder.append(String.format(formatter, cities[index]));
builder.append(" ");
}
builder.append("|");
builder.append(System.lineSeparator());
return builder;
}
private StringBuilder createBlankNameLine(int maxLength) {
String formatter = "%-" + maxLength + "s";
StringBuilder builder = new StringBuilder();
for (int index = 0; index < 5; index++) {
builder.append("| ");
builder.append(String.format(formatter, " "));
builder.append(" ");
}
builder.append("|");
builder.append(System.lineSeparator());
return builder;
}
private int getMaximumLength(String[] cities) {
int maxLength = 0;
for (int index = 0; index < cities.length; index++) {
maxLength = Math.max(maxLength, cities[index].length());
}
return maxLength;
}
}
评论
cities
char
String[] cities = new String[10];
cities[0] = "Cario"