微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在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^}^}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?