【发布时间】: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。