C语言 : Loop Text with Random Color Combination

标签 c text colors dev-c++

以下测试失败,因为“system(”color #code);“需要是静态的。 例如:-

system("color 0F"); 有效。

system("color %d %d",a, b); 没有。

完整代码:

#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>

int main() 
{
char R[15]={'1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

    while(1)
    {
    srand(time(NULL));
    int Ra = rand() %10 + 6;
    int Rb = rand() %10 + 6;

    system("color %c%c",R[Ra], R[Rb]);  

    printf("Hello world!");
    system("cls");
    };

return 0;
}

我如何让它工作?有没有更好的办法?

最佳答案

system() 不接受格式化字​​符串。

试试这个:

int main(void){

    char R[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

    srand(time(NULL));
    char color_string[20];

    while(1) {

        int Ra = rand() %16;
        int Rb = rand() %16;

        sprintf(color_string, "color %c%c", R[Ra], R[Rb]);
        system(color_string);  

        printf("Hello world!");
        system("cls");
    };

    return 0;
}

使用 getch()(按照要求,但不是标准):

while( getch() != 27) {

    system("cls");

    int Ra = rand() %16;
    int Rb = rand() %16;

    sprintf(color_string, "color %c%c", R[Ra], R[Rb]);

    system(color_string);  

    printf("Hello world!");
};

关于C语言 : Loop Text with Random Color Combination,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34641962/

相关文章:

python - 写入文本文件的最后一行?

Android Studio - 跨多个设备的文本大小问题

java - 将图像转换为颜色数组

c - 将寄存器转储到堆栈中以进行保守的堆栈扫描

objective-c - 正确减去浮点值

c - 查找txt文件中的所有数字

c - C 字符串中的 %d 个指针到整数的转换?

mongodb - MongoDB免费文本搜索和突出显示

c++ - SFML 拾色器问题

c# - 在 C# 中本地化颜色名称