如何在Eclipse控制台窗口中显示unicode字符?

Ben*_*der 6 java eclipse unicode

我正在创建一个小型Java应用程序,该应用程序使用Unicode字符在Eclipse Keplar的控制台窗口中创建框的行和列。我的代码可以正常工作,但是我打印的每个Unicode字符的输出都是一个小框,而不是我要打印的Unicode字符。

我的代码如下。我有两节课。

我的主班:

package assign03;

import java.util.Scanner;

public class Assign03 {

private static int columns;
private static int cWidth;
private static int rows;
private static int rHeight;
private static Table table;

public static void main(String[] args) {
        table = new Table();
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the number of columns: ");
        columns = input.nextInt();

        System.out.print("Enter the width of the columns: ");
        cWidth = input.nextInt();

        System.out.print("Enter the number of rows: ");
        rows = input.nextInt();

        System.out.print("Enter the width of the rows: ");
        rHeight = input.nextInt();

        table.printFirstLine(columns, cWidth);

        for (int r = 0; r < rows; r++) {
            for (int rh = 0; rh < rHeight; rh++) {
                table.printVerticalLine(columns, cWidth);
            }
            if (r < (rows - 1)
                table.printInternalLine(columns, cWidth);
        }

        table.printLastLine(columns, cWidth);
        input.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

还有我的Table.java类:

package assign03;

public class Table {

    private char firstLine[] = { '\u2501', '\u250F', '\u2533', '\u2513' };
    private char internalLine[] = { '\u2501', '\u2523', '\u254B', '\u252B' };
    private char lastLine[] = { '\u2501', '\u2517', '\u253B', '\u251B' };
    private char verticalLine[] = { '\u2503' };
    private char whiteSpace[] = { '\u2003' };

    public void printFirstLine(int columns, int cWidth) {

        System.out.print(firstLine[1]);

        for (int c = 0; c < columns; c++) {
            for (int cw = 0; cw < cWidth; cw++) {
                System.out.print(firstLine[0]);
            }
            if (c < (columns - 1))
                System.out.print(firstLine[2]);
        }

        System.out.println(firstLine[3]);
    }

    public void printVerticalLine(int columns, int cWidth) {
        for (int c = 0; c <= columns; c++) {
            System.out.print(verticalLine[0]);
            for (int cw = 0; cw < cWidth; cw++) {
                System.out.print(whiteSpace[0]);
            }
        }
        System.out.println();
    }

    public void printInternalLine(int columns, int cWidth) {
        System.out.print(internalLine[1]);

        for (int c = 0; c < columns; c++) {
            for (int w = 0; w < cWidth; w++) {
                System.out.print(internalLine[0]);
            }
            if (c < (columns - 1))
                System.out.print(internalLine[2]);
        }

        System.out.println(internalLine[3]);
    }

    public void printLastLine(int columns, int cWidth) {

        System.out.print(lastLine[1]);

        for (int c = 0; c < columns; c++) {
            for (int w = 0; w < cWidth; w++) {
                System.out.print(lastLine[0]);
            }
            if (c < (columns - 1))
                System.out.print(lastLine[2]);
        }

        System.out.println(lastLine[3]);
    }
}
Run Code Online (Sandbox Code Playgroud)

当应用程序运行时,输出如下。我对列,行,列宽和行宽使用4输入。

控制台输出

我的控制台输出看起来像什么原因?有人可以帮助我让Unicode正确显示吗?谢谢。

Dor*_*dus 7

对我有用的是转到工作区下的首选项,并将“文本文件编码”更改为 UTF-8。

  • 这也有效。您可以在(Eclipse 2018-12)下找到确切的设置:“窗口 - 首选项 - 常规 - 工作区 - 文本文件编码”(窗口左下角)。 (2认同)

Aar*_*lla 6

小方框表示当前字体没有这些符号。

您将必须找到支持它们的字体并配置控制台字体(在“控制台”的首选项中搜索)以使用它。

如果仍然无法正常运行,请确保已配置正在运行的Java应用程序(即打印到控制台的Java应用程序)以发出UTF-8。在Eclipse中,您可以通过打开启动配置并将“控制台编码”设置为UTF-8来做到这一点请参阅此博客文章此处