簡體   English   中英

帶縮進的UTF-8字符的printf

[英]printf of an UTF-8 character with indentation

我正在編寫一個掃雷文本接口,作為大學項目的作業。 我想嘗試使用此Unicode字符⚑(U + 2691)作為標記地雷的標志,並且它確實有效,有時...這就是結果。 當只有一個標志時,矩陣縮進良好,但是當附近有兩個標志時,就好像它們之間的間距較小。 我很確定控制台在打印完標志后會繼續使用不同的編碼,直到遇到不同的類型(在我的情況下為整數)。 無論如何,我是Java的新手,而且經驗不足。

兩個分開的標志與兩個連續的標志: 圖片

我有兩個矩陣,一個是存放所有炸彈和炸彈在其附近的矩陣,另一個是指示是否已發現某個單元的狀態:特別是,如果狀態矩陣中的一個單元== 0,則它可用,如果它是== 1,則會被發現,如果它是== 2,則意味着用戶在該單元格中放置了一個標志。 撇開用於矩陣生成的代碼,用於發現所有連續零的遞歸方法以及枚舉矩陣的行和列的方法,這是代碼中有趣的部分:

public class Minesweeper {

    //possible initial configuration
    private int [][] state = new int [10][10];
    private int [][] field = {  
        { 0, 1,-1,-1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 2, 2, 1, 0, 1,-1, 1, 0},
        { 0, 1, 2, 3, 2, 1, 1, 1, 2, 1},
        { 0, 1,-1,-1,-1, 1, 0, 0, 2,-1},
        { 0, 1, 2, 3, 2, 1, 0, 0, 1,-1},
        { 0, 0, 0, 0, 1, 1, 2, 1, 3, 2},
        { 0, 0, 0, 0, 1,-1, 2,-1, 2,-1},
        { 1, 2, 1, 0, 1, 1, 3,-1, 2, 1},
        {-1, 3,-1, 2, 1, 2, 2, 2, 1, 0},
        {-1, 3, 1, 2,-1, 2,-1, 0, 0, 0}};

    public void printField() {

        String FLAG = "⚑";  //or "⚐"

        for (int row = 0; row < field.length; row++) {

            for (int col = 0; col < field[row].length; col++) {
                if (state[row][col] == 0)
                    System.out.printf("%4c", '-');
                if (state[row][col] == 1 && field[row][col] != -1)
                    System.out.printf("%4d", field[row][col]);
                if (field[row][col] == -1 && state[row][col] == 1)
                    System.err.printf("%4c", '*');
                if (state[row][col] == 2) {
                    System.err.printf("%4c" , FLAG);
                }
            }
            System.out.println();
        }

    }

    public static void main (String [] args) {
        Minesweeper campo = new Minesweeper ();
        campo.printField();
    }
}

如果使用chars和unicode值來標記\⚑似乎工作得很好( 請參閱此處

這是一個簡化的版本:

private static int [][] field = {
    { 0, 1,-1,-1, 1, 0, 1, 1, 1, 0},
    { 0, 1, 2, 2, 1, 0, 1,-1, 1, 0},
    { 0, 1, 2, 3, 2, 1, 1, 1, 2, 1},
    { 0, 1,-1,-1,-1, 1, 0, 0, 2,-1},
    { 0, 1, 2, 3, 2, 1, 0, 0, 1,-1},
    { 0, 0, 0, 0, 1, 1, 2, 1, 3, 2},
    { 0, 0, 0, 0, 1,-1, 2,-1, 2,-1},
    { 1, 2, 1, 0, 1, 1, 3,-1, 2, 1},
    {-1, 3,-1, 2, 1, 2, 2, 2, 1, 0},
    {-1, 3, 1, 2,-1, 2,-1, 0, 0, 0}};

    public static void main (String[] args) {

    Character FLAG = '\u2691';

    for (int row = 0; row < field.length; row++) {
        for (int col = 0; col < field[row].length; col++) {
            int f = field[row][col];
            char c = '-';
            if (f == 1)
                c = Character.forDigit(f, 10);
            else if (f == -1)
                c = '*';
            else if (f == 2) {
                c = FLAG;
            }
            System.out.printf("%4c" , c);
        }
        System.out.println();
    }
}

我明白了:

   -   1   *   *   1   -   1   1   1   -
   -   1   ⚑   ⚑   1   -   1   *   1   -
   -   1   ⚑   -   ⚑   1   1   1   ⚑   1
   -   1   *   *   *   1   -   -   ⚑   *
   -   1   ⚑   -   ⚑   1   -   -   1   *
   -   -   -   -   1   1   ⚑   1   -   ⚑
   -   -   -   -   1   *   ⚑   *   ⚑   *
   1   ⚑   1   -   1   1   -   *   ⚑   1
   *   -   *   ⚑   1   ⚑   ⚑   ⚑   1   -
   *   -   1   ⚑   *   ⚑   *   -   -   -

我在Linux上運行它。 在IntelliJ控制台中和在終端上,兩者的縮進效果都很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM