【问题标题】:C - Triangle program not printing any output on call to mainC - 三角程序在调用 main 时不打印任何输出
【发布时间】:2018-10-14 18:25:42
【问题描述】:

我编写了一个应该打印三角形的 C 代码。定义了宽度和字符,然后代码应打印出填充该字符的三角形。我认为main中可能有错误,但代码编译得很好。当我运行它时,没有输出。

这是我的代码:

void triangle(int width, char x);

int main(void){
    triangle(4, c);
}

void triangle(int width, char x){
    if (width > 2){
        return;
    }
    int counter = 1;
    int direction = 1;
    do{
        int i;
        for(i = 0; i < width - counter; i++){
            printf(" ");
        }
        for (int i = 0; i < counter; i++){
            printf("%c", x);
        }
        printf("\n");
        counter += direction;
        if(counter > width){
            counter = width - 1;
            direction = -1;
        }
    }while (counter != 1);
    return;
}

【问题讨论】:

  • 预期输出是什么?
  • 您的代码中有多个错误:c 是什么?全局变量?如果你想将它作为字符传递,它应该是 'c'。然后您的三角形函数将立即退出,因为宽度 (4) 大于 2。

标签: c printf main


【解决方案1】:

你有

 if (width > 2){
        return;
 }

电话是

 triangle(4, c);

这解释了无输出。 还有其他错误,但您可能先尝试自己解决它们?

【讨论】:

    【解决方案2】:

    抱歉,如果问题中不清楚。 预期的输出应该是一个宽度(和高度)为 4 的三角形,填充为“c”。

    所以我想我已经想通了。 我必须使用

    【讨论】:

      猜你喜欢
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 2021-10-12
      • 2011-10-22
      相关资源
      最近更新 更多