我的任务是使用两种不同的循环(For,While,do While)。任务是要求用户输入1到10之间的数字,然后程序将从0到用户数。此外,程序必须能够显示错误消息,并且如果用户输入1到10之外的数字,则要求用户再次输入数字。代码中包含错误消息的部分以及再次输入数字的提示工作得很好。但是,当我输入一个范围内的数字时,它要么不做任何事情,要么从0到它们的数字计数无限次,并且不会停止循环计数。请帮忙!
#include <stdio.h>
int main(void)
{
//Variables
int num;
int zero;
//Explains to the user what the program will do
printf("This program will count from 0 to a number you pick.\n\n");
//Asks the user to input a value
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
//If the correct range was selected by the user
while ( num >= 1 && num <= 10 )
{
for ( zero = 0; zero <= num; zero++ )
{
printf("%d...", zero);
}
}
//If a value outside of the accepted range is entered
while ( num < 1 || num > 10)
{
printf("I'm sorry, that is incorrect.\n");
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
}
printf("\n\n\n");
return 0;
}
答案 0 :(得分:1)
while ( num >= 1 && num <= 10 )
{
for ( zero = 0; zero <= num; zero++ )
{
printf("%d...", zero);
}
}
如果num在1到10之间,那么将永远运行,因为在循环内部没有更改num - 一旦你进入,你就处于良好状态。
如果输入“坏”值,那么你将跳过这个并进入第二个while循环。然而,一旦你通过输入一个“好”值离开那个循环,那么剩下要执行的就是
printf("\n\n\n");
return 0;
你需要摆脱第一个while循环并将第二个循环移动到for循环之上:
#include <stdio.h>
int main(void)
{
//Variables
int num;
int zero;
//Explains to the user what the program will do
printf("This program will count from 0 to a number you pick.\n\n");
//Asks the user to input a value
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
//If a value outside of the accepted range is entered
while ( num < 1 || num > 10)
{
printf("I'm sorry, that is incorrect.\n");
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
}
for ( zero = 0; zero <= num; zero++ )
{
printf("%d...", zero);
}
printf("\n\n\n");
return 0;
}
答案 1 :(得分:0)
将两个while循环更改为if guard,
num=1;
while(num>0)
{
//Asks the user to input a value
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
//If the correct range was selected by the user
if ( num >= 1 && num <= 10 )
{
for ( zero = 0; zero <= num; zero++ )
{
printf("%d...", zero);
}
}
//If a value outside of the accepted range is entered
if ( num < 1 || num > 10)
{
printf("I'm sorry, that is incorrect.\n");
printf("Please enter a number (between 1 and 10): \n");
//scanf("%d", &num);
}
while(0); while(0); while(0); while(0); //gratuitous loops
do ; while(0); for(;0;);
}
答案 2 :(得分:0)
真的很简单!
你可以在while循环中使用 break :
#include <stdio.h>
int main(void)
{
//Variables
int num;
int zero;
//Explains to the user what the program will do
printf("This program will count from 0 to a number you pick.\n\n");
//Asks the user to input a value
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
//If the correct range was selected by the user
while ( num >= 1 && num <= 10 )
{
for ( zero = 0; zero <= num; zero++ )
{
printf("%d...", zero);
}
break; // !!! Here !!!
}
//If a value outside of the accepted range is entered
while ( num < 1 || num > 10)
{
printf("I'm sorry, that is incorrect.\n");
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
}
printf("\n\n\n");
return 0;
}