Pset 1(Mario Less Hard)
Pset 1 (Mario Less Hard)
这个程序不 运行 它只是询问高度然后 there.When 我 运行 检查 50 它告诉我像 foo 和其他 a 这样的值被接受了所以我用 scanf 替换了 printf,一切都停止了。
还有人可以指导我如何使程序不断询问 2 到 8 之间的高度直到它为真吗?截至目前,它会发出一条消息,要求输入 2 和 8 之间的数字并停止。
int main()
{
int height;
do {
height =get_int("Height: ");
scanf("%d", &height);
if ( height<1 || height>8) {
printf ("Kindly Enter A Number Between 2 & 8 !\n");
return 0;
}
}
while (height <1 || height>8);
for(int i=0; i<height ; i++) // Row Number
{
for (int j=0; j<height ;j++)
{
if (i+j >= height-1)
printf("#");
else
printf (" ");
}
printf("\n");
}
}
删除if
语句中的return 0;
语句。 return
函数中的 main
语句立即终止程序。
为什么服用 height
两次:
height =get_int("Height: ");
scanf("%d", &height);
这就足够了:scanf("%d", &height);
.
也总是检查 scanf
的 return。
并如前所述删除 return 0
,否则当您到达它时,您的程序将立即结束。
我假设你想要的是这样的:
int height;
do {
if (scanf("%d", &height) == 1);//checking the result of scanf function
else
exit(EXIT_FAILURE);
if (height < 1 || height>8) {
printf("Kindly Enter A Number Between 2 & 8 !\n");
//removing return 0
}
}
while (height < 1 || height>8);
这个程序不 运行 它只是询问高度然后 there.When 我 运行 检查 50 它告诉我像 foo 和其他 a 这样的值被接受了所以我用 scanf 替换了 printf,一切都停止了。
还有人可以指导我如何使程序不断询问 2 到 8 之间的高度直到它为真吗?截至目前,它会发出一条消息,要求输入 2 和 8 之间的数字并停止。
int main()
{
int height;
do {
height =get_int("Height: ");
scanf("%d", &height);
if ( height<1 || height>8) {
printf ("Kindly Enter A Number Between 2 & 8 !\n");
return 0;
}
}
while (height <1 || height>8);
for(int i=0; i<height ; i++) // Row Number
{
for (int j=0; j<height ;j++)
{
if (i+j >= height-1)
printf("#");
else
printf (" ");
}
printf("\n");
}
}
删除if
语句中的return 0;
语句。 return
函数中的 main
语句立即终止程序。
为什么服用 height
两次:
height =get_int("Height: ");
scanf("%d", &height);
这就足够了:scanf("%d", &height);
.
也总是检查 scanf
的 return。
并如前所述删除 return 0
,否则当您到达它时,您的程序将立即结束。
我假设你想要的是这样的:
int height;
do {
if (scanf("%d", &height) == 1);//checking the result of scanf function
else
exit(EXIT_FAILURE);
if (height < 1 || height>8) {
printf("Kindly Enter A Number Between 2 & 8 !\n");
//removing return 0
}
}
while (height < 1 || height>8);