【发布时间】:2015-12-09 14:03:08
【问题描述】:
这会在输入高度时创建一个类似马里奥的金字塔。
#include <stdio.h>
int main(void)
{
int height;
char signal = 'n';
while (signal != 'y')
{
printf("Height: ");
scanf("%d", &height);
if (height <= 0)
{
printf("Error. Positive #s only. \n");
}
else if (height > 23)
{
printf("Error. # must be from 0-23. \n");
}
else if (height > 0 || height <= 23)
{
signal = 'y';
}
else
{
printf("Can you bulid a pyramid with letters? I thought so... \n");
}
}
int i, j, k;
for (i = 1; i <= height; i++)
{
if (height <= 8)
printf("\t");
else if (height <= 16)
printf("\t\t");
else if (height <= 23)
printf("\t\t\t");
for (k = 0; k < i; k++)
{
printf("\b");
}
for (j = 0; j <= i; j++)
{
printf("#");
}
printf("\n");
}
}
如果用户决定做傻事并输入一个字符串,程序就会进入无限循环。我以为程序会使用 else 语句,但没有。我该如何处理?
【问题讨论】:
-
请编辑您的帖子并修正缩进。
标签: c string error-handling