我的任务是使用两种不同的循环(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;
}