【问题标题】:Changing the color of a specific character更改特定字符的颜色
【发布时间】:2016-03-04 00:07:57
【问题描述】:

有什么方法可以改变特定角色控制台的颜色吗? 我正在使用代码块,例如,我想将所有 @ 的颜色更改为红色,将所有 o 的颜色更改为黄色。

【问题讨论】:

    标签: c colors character


    【解决方案1】:

    你必须编写一个不同的函数来完成这个任务。我正在添加一个代码来展示它是如何在 C 中完成的。`

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    void output(char *s)
    {
    int i=0;
    while(*(s+i) !='\0')
    {
        if(*(s+i)=='@')
       {
       textcolor(RED);
        cprintf("%c",*(s+i));
       }
       else if(*(s+i) =='.')
       {
        textcolor(YELLOW);
        cprintf("%c",*(s+i));
       }
       else
       {
       textcolor(WHITE);
       cprintf("%c",*(s+i));
       }
       i++;
    }
    }
    void main()
    {
    char S[]="@shvet.";
    output(S);
    getch();
    }
    

    这是输出控制台窗口的图像。

    请注意,我使用的是 cprintf 函数而不是 printf。这是因为 cprintf 将格式化的输出发送到屏幕上的文本窗口,而 printf 将其发送到标准输入。

    【讨论】:

    • 如果这解决了您的问题。将答案标记为已接受。
    • 只是..CodeBlocks 或 Microsoft Visual Studio 都无法识别 textcolor 函数..
    • 特别是如果您想在代码块中执行此操作,请点击以下链接,这可能会有所帮助link
    • 谢谢!先生,您是救生员:))
    猜你喜欢
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2017-06-06
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    相关资源
    最近更新 更多