制作游戏板:你如何缩短这个创建数组的代码,你如何将它们打印出来?

MAKING GAME BOARD: How do you shorten this code of creating array, and how do you print them out?

提问人:ICIS 提问时间:5/12/2021 最后编辑:Federico klez CullocaICIS 更新时间:5/13/2021 访问量:61

问:

我正在开发一个游戏板,可以打印出这样的东西:

game board plan

这是我写的代码

    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.打印出像图像一样的数组。

不能打印漂亮的独立盒子,所以我只是把盒子做成它们在倒下的梯子里

Java 数组 打印 ArrayIndexOutofboundsException

评论

0赞 Federico klez Culloca 5/12/2021
为什么是 s 数组而不是字符串数组?citieschar
0赞 ICIS 5/12/2021
我想保存例如cities[0][0] = c(在city1 = cario中)...直到 cities[0] 拥有 cit1 的所有字符
0赞 ICIS 5/12/2021
你的意思是我必须制作'String [][] cities = new String[10][]'和'cities[0] = city1.toCharArray()'?
0赞 Federico klez Culloca 5/12/2021
不,我的意思是和.String[] cities = new String[10];cities[0] = "Cario"
0赞 ICIS 5/12/2021
数组 cities[][] 的格式类似于 { {c,a,r,i,o, , , ,...}, {s,e,o,u,l, ,} ...},所以我认为 char 是正确的......

答:

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;
    }

}