c中的系统颜色动态变化
System color dynamic changing in c
为什么我不能这样做;
char backgroundColor='c',textColor='e';
printf("Please, enter background color: "); scanf("%c",&backgroundColor);
printf("Please, enter text color: "); scanf("%c",&textColor);
system("color "+backgroundColor+textColor);
我该如何解决这个问题?
您不能在 C 中添加字符串。控制台颜色由 color BF
设置,其中 B
是背景颜色,F
是前景(文本)颜色,在十六进制。所以 color 1E
将设置蓝色背景和黄色文本。此外 scanf
需要在 %c
之前添加一个 space,如此处所示,以清除 newline
.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char backgroundColor='c',textColor='e';
char sysmes[] = "color BF";
printf("Please, enter background color: ");
scanf(" %c",&backgroundColor);
printf("Please, enter text color: ");
scanf(" %c",&textColor);
sysmes[6] = backgroundColor;
sysmes[7] = textColor;
system(sysmes);
return 0;
}
为什么我不能这样做;
char backgroundColor='c',textColor='e';
printf("Please, enter background color: "); scanf("%c",&backgroundColor);
printf("Please, enter text color: "); scanf("%c",&textColor);
system("color "+backgroundColor+textColor);
我该如何解决这个问题?
您不能在 C 中添加字符串。控制台颜色由 color BF
设置,其中 B
是背景颜色,F
是前景(文本)颜色,在十六进制。所以 color 1E
将设置蓝色背景和黄色文本。此外 scanf
需要在 %c
之前添加一个 space,如此处所示,以清除 newline
.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char backgroundColor='c',textColor='e';
char sysmes[] = "color BF";
printf("Please, enter background color: ");
scanf(" %c",&backgroundColor);
printf("Please, enter text color: ");
scanf(" %c",&textColor);
sysmes[6] = backgroundColor;
sysmes[7] = textColor;
system(sysmes);
return 0;
}