如何在Windows控制台中以红色和黑色显示这些卡牌西装符号?

问题描述

我有这一系列的西服套装。

char *suits[4] = {"♥","♦","♣","♠"};

为了能够在Windows控制台中看到它们,我必须编写:

SetConsoleOutputCP(65001);

有没有办法为这些西装设置颜色?

具体来说,红色代表♥和♦,黑色代表♣和♠。

解决方法

由于PowerShell被标记,您可以使用Write-Host将这些带有颜色的符号打印到控制台:

$suits = "♥","♦","♣","♠"
Write-Host $($suits[0..1]) -ForegroundColor Red; Write-Host $($suits[2..3]) -ForegroundColor Black

在cmd shell中运行此命令,仅表示调用PowerShell.exe以运行以下代码:

powershell.exe -command "$suits = '♥','♦','♣','♠'; Write-Host $($suits[0..1]) -ForegroundColor Red; Write-Host $($suits[2..3]) -ForegroundColor Black"
,

尝试一下!

之所以看不到它们,是因为通常cmd控制台不支持其他符号(这是Unicode标准中的特殊符号)。但是,如果直接在命令提示符下运行,则可以看到它们。您可以尝试使用此代码,也可以在此处阅读_setmode && wprintf文档! ?‍??‍?♥

// crt_setmodeunicode.c
// This program uses _setmode to change
// stdout to Unicode. Cyrillic and Ideographic
// characters will appear on the console (if
// your console font supports those character sets).

#include <fcntl.h> //file control library
#include <io.h>  //IO parameter   
#include <stdio.h>

int main(void) {
    _setmode(_fileno(stdout),_O_U16TEXT); //We're recibing the stdout file descriptor and then we change
                                           //it to the Unicode translation.
    wprintf(L" CLUB: \x2663 \n\n DIAMOND: \x2666 \n\n HEART: \x2665 \n\n SPADES: \x2660\n");
    return 0;
}
,

enter image description here

  • 对于Powershell
$suits = [char]0x2665,[char]0x2666,[char]0x2663,[char]0x2660;
0..4|% {if ($_ -le 1){ 
    write-host $suits[$_] -ForegroundColor Red –NoNewline;
     } else { write-host $suits[$_] -ForegroundColor Black –NoNewline}}

enter image description here

  • 对于cmd
powershell -nop -c $suits=[char]0x2665,[char]0x2660;0..4^|? ^{if ($_ -le 1) ^{write-host $suits[$_] -ForegroundColor Red -NoNewline^} else ^{write-host $suits[$_] -ForegroundColor Black -NoNewline^}^}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...