for循环中的for循环使用if else条件

时间:2015-12-07 07:35:26

标签: c nested-loops

for (x = 0; x < 4; x++) {
    for (int a = 22; a <= 62;) {
        if (isCooked[x] == 1) {
            gotoxy(a,3); printf("cooked");
            gotoxy(a,4); printf("%-10s",food[userServings[x]]);
            a += 12;
        } else {
            gotoxy(a,3); printf("!");
            gotoxy(a,4); printf("%-10s",food[userServings[x]]);
            a += 12;
        }
    }
}

input output

我可以问上面的循环和条件有什么问题。我正在尝试打印我选择的4种蔬菜的名称。使用gotoxy我想在我的循环上的给定坐标上打印它们。

3 个答案:

答案 0 :(得分:1)

在c中你无法在任何地方声明变量。你已经在内部for循环中声明了int。

答案 1 :(得分:1)

程序中的

未声明

答案 2 :(得分:0)

您应该只计算gotoxy坐标:

,而不是第二个循环
for (x = 0; x < 4; x++) {
    int a = 22 + x * 12;

    if (isCooked[x] == 1) {
        gotoxy(a, 3); printf("cooked");
        gotoxy(a, 4); printf("%-10s", food[userServings[x]]);
    } else {
        gotoxy(a, 3); printf("!     ");
        gotoxy(a, 4); printf("%-10s", food[userServings[x]]);
    }
}
相关问题