首页 > 解决方案 > Cout 在 for 循环中使用不正确的变量值执行(同时也失败了?)

问题描述

我只是想知道是否有人知道如何解决这个问题,我正在尝试为我正在开发的另一个项目获取一个基本上“打印代码”的输出,以便用户可以输入文本框的颜色,但是每当我尝试时,它总是打印:

if(当前x 值== x 循环的当前迭代)和(当前 y 值== y 循环的当前迭代

cout<<系统(颜色08)

(之间没有空格,256 次。)这里的一切都很好,除了 cout 语句的 system(Color) 部分它总是 08,当 c1 和 c2 每次都应该改变时。任何帮助将不胜感激,感谢您的宝贵时间!

using namespace std;
int main()
{
    int c1, c2;
    for (int x=1; x<=16; x++)
    {
        if (x==1)
        int c1=1;
        else if (x==2)
        int c1=2;
        else if (x==3)
        int c1=3;
        else if (x==4)
        int c1=4;
        else if (x==5)
        int c1=5;
        else if (x==6)
        int c1=6;
        else if (x==7)
        int c1=7;
        else if (x==8)
        int c1=8;
        else if (x==9)
        int c1=9;
        else if (x==10)
        int c1=0;
        else if (x==11)
        char c1='A';
        else if (x==12)
        char c1='B';
        else if (x==13)
        char c1='C';
        else if (x==14)
        char c1='D';
        else if (x==15)
        char c1='E';
        else if (x==16)
        char c1='F';
        for (int y=1; y<=16; y++)
        {
            if (y==1)
            int c2=1;
            else if (y==2)
            int c2=2;
            else if (y==3)
            int c2=3;
            else if (y==4)
            int c2=4;
            else if (y==5)
            int c2=5;
            else if (y==6)
            int c2=6;
            else if (y==7)
            int c2=7;
            else if (y==8)
            int c2=8;
            else if (y==9)
            int c2=9;
            else if (y==10)
            int c2=0;
            else if (y==11)
            char c2='A';
            else if (y==12)
            char c2='B';
            else if (y==13)
            char c2='C';
            else if (y==14)
            char c2='D';
            else if (y==15)
            char c2='E';
            else if (y==16)
            char c2='F';
            cout<<flush<<"if (x=="<<x<<") and (y=="<<y<<")\nsystem(Color "<<c1<<c2<<")"<<endl;
        }
    }
    //cout<<c1<<" "<<c2;```

The now working code (Thanks François Andrieux!!!):

'''#include<iostream>
using namespace std;
int main()
{
    char c1, c2;
    for (int x=1; x<=16; x++)
    {
        if (x==1)
        c1='1';
        else if (x==2)
        c1='2';
        else if (x==3)
        c1='3';
        else if (x==4)
        c1='4';
        else if (x==5)
        c1='5';
        else if (x==6)
        c1='6';
        else if (x==7)
        c1='7';
        else if (x==8)
        c1='8';
        else if (x==9)
        c1='9';
        else if (x==10)
        c1='0';
        else if (x==11)
        c1='A';
        else if (x==12)
        c1='B';
        else if (x==13)
        c1='C';
        else if (x==14)
        c1='D';
        else if (x==15)
        c1='E';
        else if (x==16)
        c1='F';
        for (int y=1; y<=16; y++)
        {
            if (y==1)
            c2='1';
            else if (y==2)
            c2='2';
            else if (y==3)
            c2='3';
            else if (y==4)
            c2='4';
            else if (y==5)
            c2='5';
            else if (y==6)
            c2='6';
            else if (y==7)
            c2='7';
            else if (y==8)
            c2='8';
            else if (y==9)
            c2='9';
            else if (y==10)
            c2='0';
            else if (y==11)
            c2='A';
            else if (y==12)
            c2='B';
            else if (y==13)
            c2='C';
            else if (y==14)
            c2='D';
            else if (y==15)
            c2='E';
            else if (y==16)
            c2='F';
            cout<<flush<<"if (x=="<<x<<") and (y=="<<y<<")\nsystem(Color "<<c1<<c2<<")"<<endl;
        }
    }
    //cout<<c1<<" "<<c2;
}'''

标签: c++for-loopcout

解决方案


推荐阅读