當輸入高度時,會創建一個類似馬里奧的金字塔。處理錯誤,無限循環
#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語句,但不會。我該如何處理?
請編輯您的帖子和修復縮進。 – Lundin