如何在 Java 中打印 Unicode 字符?

问题描述 投票:0回答:1

我有一个名为“Card”的类,它存储一个字符和一个整数。我将卡片的 unicode 字符(Unicode 值取自此维基百科页面:https://en.wikipedia.org/wiki/Playing_cards_in_Unicode)传递到卡片实例中。下面是班级的样子。

import java.util.ArrayList;
import java.util.Collections;

public class Card {
    protected char unicodeCard;
    protected int value;

    public Card() {}

    public Card(char unicodeCard, int value) {
        this.unicodeCard = unicodeCard;
        this.value = value;
    }

    public ArrayList<Card> buildDeck() {
        ArrayList<Card> deck = new ArrayList<>();
        deck.add(new Card('\uF0A1', 11));
        deck.add(new Card('\uF0B1', 11));
        deck.add(new Card('\uF0C1', 11));
        deck.add(new Card('\uF0D1', 11));
        deck.add(new Card('\uF0A2', 2));
        deck.add(new Card('\uF0B2', 2));
        deck.add(new Card('\uF0C2', 2));
        deck.add(new Card('\uF0D2', 2));
        deck.add(new Card('\uF0A3', 3));
        deck.add(new Card('\uF0B3', 3));
        deck.add(new Card('\uF0C3', 3));
        deck.add(new Card('\uF0D3', 3));
        deck.add(new Card('\uF0A4', 4));
        deck.add(new Card('\uF0B4', 4));
        deck.add(new Card('\uF0C4', 4));
        deck.add(new Card('\uF0D4', 4));
        deck.add(new Card('\uF0A5', 5));
        deck.add(new Card('\uF0B5', 5));
        deck.add(new Card('\uF0C5', 5));
        deck.add(new Card('\uF0D5', 5));
        deck.add(new Card('\uF0A6', 6));
        deck.add(new Card('\uF0B6', 6));
        deck.add(new Card('\uF0C6', 6));
        deck.add(new Card('\uF0D6', 6));
        deck.add(new Card('\uF0A7', 7));
        deck.add(new Card('\uF0B7', 7));
        deck.add(new Card('\uF0C7', 7));
        deck.add(new Card('\uF0D7', 7));
        deck.add(new Card('\uF0A8', 8));
        deck.add(new Card('\uF0B8', 8));
        deck.add(new Card('\uF0C8', 8));
        deck.add(new Card('\uF0D8', 8));
        deck.add(new Card('\uF0A9', 9));
        deck.add(new Card('\uF0B9', 9));
        deck.add(new Card('\uF0C9', 9));
        deck.add(new Card('\uF0D9', 9));
        deck.add(new Card('\uF0AA', 10));
        deck.add(new Card('\uF0BA', 10));
        deck.add(new Card('\uF0CA', 10));
        deck.add(new Card('\uF0DA', 10));
        deck.add(new Card('\uF0AB', 10));
        deck.add(new Card('\uF0BB', 10));
        deck.add(new Card('\uF0CB', 10));
        deck.add(new Card('\uF0DB', 10));
        deck.add(new Card('\uF0AD', 10));
        deck.add(new Card('\uF0BD', 10));
        deck.add(new Card('\uF0CD', 10));
        deck.add(new Card('\uF0DD', 10));
        deck.add(new Card('\uF0AE', 10));
        deck.add(new Card('\uF0BE', 10));
        deck.add(new Card('\uF0CE', 10));
        deck.add(new Card('\uF0DE', 10));
        return deck;
    }

    public void shuffle(ArrayList<Card> deck) {
        Collections.shuffle(deck);
    }

    public char getCardChar() {
        return this.unicodeCard;
    }
}

在我的 main 方法中,我尝试打印 ArrayList 中的 Unicode 字符,但我从控制台收到的只是一个 ?特点。此外,我尝试创建一个将编码设置为“UTF-8”的 PrintStream 实例,但这似乎也不起作用。我只得到如下所示的输出:∩é▒。下面是我的主要方法。

public static void main(String[] args) throws UnsupportedEncodingException {
        PrintStream out = new PrintStream(System.out, true, "UTF-8");
        Card card = new Card();
        ArrayList<Card> deck = card.buildDeck();
        for (int i = 0; i < deck.size(); i++) {
            out.println(deck.get(i).getCardChar());
        }
    }

如果我可以提供任何其他信息,请告诉我。预先感谢您的任何帮助! :)

java unicode stdout printstream
1个回答
0
投票

尝试使用

\uD83C\uDCA1
来获得黑桃 A。

https://www.fileformat.info/info/unicode/char/1f0a1/index.htm

© www.soinside.com 2019 - 2024. All rights reserved.