简体   繁体   English

在 Java 中打印 Unicode 字符 - PrintWriter

[英]Printing Unicode Characters in Java - PrintWriter

["

I am developing a card game in Java and I am currently using a command line to interact and display outputs of the game.<\/i>我正在用 Java 开发纸牌游戏,我目前正在使用命令行来交互和显示游戏的输出。<\/b> I have been representing each card suit with A letter (H - Hearts, S - Spades etc.)<\/i>我一直用字母代表每张纸牌(H - 红心,S - 黑桃等)<\/b><\/p>

I came up with the idea of using the Unicode values rather than letters to show each suit<\/i>我想出了使用 Unicode 值而不是字母来显示每件西装的想法<\/b><\/p>

public String toSymbol(Suit suit){
    switch(suit){
        case SPADE:
            return "\u2664";
        case DIAMOND:
            return "\u2662";
        case CLUB:
            return "\u2667";
        case HEART:
            return "\u2661";
        default:
            return "null";
    }
}

Glyph字形

As commented, you should check that your System.out and console are set to UTF-8, and that a font is available containing glyphs for those characters.正如所评论的,您应该检查您的System.out和控制台是否设置为 UTF-8,并且是否有包含这些字符的字形的字体可用。

You neglected to mention your operating system, but I'll guess it is Microsoft Windows.您忽略了提及您的操作系统,但我猜它是 Microsoft Windows。 If so, this Question might help: Java, UTF-8, and Windows console .如果是这样,这个问题可能会有所帮助: Java、UTF-8 和 Windows 控制台

We can simplify your code.我们可以简化您的代码。 Java source code supports UTF-8, so you can use the actual suit characters directly rather then use the Unicode code point escapes. Java 源代码支持 UTF-8,因此您可以直接使用实际的西装字符,而不是使用 Unicode 代码点转义。

Here are the four French-suited characters, in “black” and in “white”.这是四个法国西装字符,“黑色”和“白色”。

System.out.println( "♠️♥️♣️♦️" ) ; 
System.out.println( "♤♡♧♢" ) ; 

See this code run successfully at Ideone.com .请参阅此代码在 Ideone.com 上成功运行

Enum枚举

By the way, you can embed knowledge of the emoji character within your enum.顺便说一句,您可以在枚举中嵌入表情符号字符的知识。 No need to maintain this separate method as shown in your Question.如您的问题所示,无需维护这种单独的方法。

enum Suit {
    SPADES( "♠️" , "♤" ), HEARTS( "♥️" , "♡" ), CLUBS( "♣️" , "♧" ), DIAMONDS( "♦️" , "♢" );
    private final String emojiBlack, emojiWhite ;
    Suit( String emojiBlack , String emojiWhite ) {  // Constructor.
        this.emojiBlack = emojiBlack ;
        this.emojiWhite= emojiWhite ;
    }
    String emojiBlack() {  // Accessor (getter).
        return this.emojiBlack ;
    }
    String emojiWhite() {  // Accessor (getter).
        return this.emojiWhite ;
    }
}

Usage:用法:

System.out.println(
    Suit.HEARTS.emojiBlack() 
);

♥️ ♥️

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM