文字背景颜色更改后C 中的许多循环崩溃!为什么

Text Background Color Changing Collapses after many loops in C++! Why?

本文关键字:许多 循环 崩溃 为什么 颜色 背景 文字      更新时间:2023-10-16

运行此代码后,我注意到,如果循环超过特定数字,背景着色崩溃了。一个人知道为什么吗?代码在C

#include <iostream>
using namespace std;

这是着色函数TextColor(参数1,参数2)该功能作为参数0-15和128的整数数字第一个参数用于Fontcolor和第二个参数用于背景颜色

#include <Windows.h>
#define BLACK 0
#define BLUE 1
#define GREEN 2
#define CYAN 3
#define RED 4
#define MAGENTA 5
#define BROWN 6
#define LIGHTGREY 7
#define DARKGREY 8
#define LIGHTBLUE 9
#define LIGHTGREEN 10
#define LIGHTCYAN 11
#define LIGHTRED 12
#define LIGHTMAGENTA 13
#define YELLOW 14
#define WHITE 15
#define BLINK 128
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
void TextColor(int fontcolor, int backgroundcolor)
{
int color_attribute;
color_attribute = backgroundcolor;
color_attribute = _rotl(color_attribute, 4) | fontcolor;
SetConsoleTextAttribute(screen, color_attribute);
}

//测试着色功能的主要程序

int main()
{

int counter = 1;
while(counter<=50)
{ 
    TextColor(0, 15);
    int i = 0, j;
    while (i <= 5)
    {
        j = 1;
        while (j <= 28)
        {
            cout << " ";
            j++;
        }
        cout << endl;
        i++;
    }
    TextColor(15, 0);
    cout << endl << endl << endl;
    counter++;
}
system("pause");
return 0;
}

新图像仅1层面颜色

为此行:

TextColor(0, counter%20);

如果唯一的有效颜色在0-15之间,那么当counter%20表达式评估为16-19之间时,您期望发生什么?