有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何删除打印行之间的空格?

我创建这个程序是为了在我正在创建的文本冒险中帮助绘制地图。到目前为止,它所做的只是画一个你输入的拘留的黑色正方形。有没有办法消除每条线之间的空隙,这样就不会有白线穿过正方形

这是我的密码:

import java.util.Scanner;

public class MapGrid {

    static String createGrid(int x, int y) {
        String output = "";
        String block = new String("\u2588\u2588"); //A string of two unicode block symbols: ██

        if(x * y != 0) { //If neither x nor y is 0, a map of length x and height y will be returned
            for(int n = 1; n <= y; n++) {
                for(int i = 1; i <= x; i++) {
                    output += block;
                }
                output += "\n";
            }
        }
        return output;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try(Scanner sc = new Scanner(System.in)) {

            System.out.println("Please enter the length of your map");
            int length = sc.nextInt();

            System.out.println("Please enter the height of your map");
            int height = sc.nextInt();

            System.out.println(createGrid(length,height));
        }
    }

}

这是用户输入5和5时打印的内容:

enter image description here

我拍了一张截图,因为用这种字体很难看到我在说什么

在每一个新的块行之间都有一个小的间隙,这使得它看起来不正确。也许没有办法解决这个问题,但我想我会问一下,以防万一


共 (1) 个答案

  1. # 1 楼答案

    您可能想将System.out重定向到其他位置
    如果您想使用Swing,那么您可以查看here

    那么,这是另一个地方的责任,在那里你重定向它
    但现在它只是IDE的一部分(Eclipse)。它独立于Java逻辑

    如果你认真考虑的话。您不想在EclipseIDE中玩文本冒险游戏。因此,如果您使用的是窗口,下一个问题是是否要使用窗口命令行。这对你来说可能已经足够了。但我仍然建议您首先考虑在哪种窗口中进行文本输出