C 为什么赢了';我的程序不能停止循环吗?
我的任务是使用两种不同类型的循环(For、While、do-While)。任务是要求用户输入一个介于1和10之间的数字,然后程序将从0计数到用户数。此外,如果用户输入的数字超出1到10,程序必须能够显示错误消息并要求用户再次输入数字。代码中包含错误消息和再次输入数字的提示的部分工作正常。但是,当我在范围内输入一个数字时,它要么不执行任何操作,要么从0到其数字进行无限次计数,并且不会停止循环计数。请帮忙C 为什么赢了';我的程序不能停止循环吗?,c,loops,for-loop,while-loop,putty,C,Loops,For Loop,While Loop,Putty,我的任务是使用两种不同类型的循环(For、While、do-While)。任务是要求用户输入一个介于1和10之间的数字,然后程序将从0计数到用户数。此外,如果用户输入的数字超出1到10,程序必须能够显示错误消息并要求用户再次输入数字。代码中包含错误消息和再次输入数字的提示的部分工作正常。但是,当我在范围内输入一个数字时,它要么不执行任何操作,要么从0到其数字进行无限次计数,并且不会停止循环计数。请帮忙 #include <stdio.h> int main(void) {
#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;
}
#包括
内部主(空)
{
//变数
int-num;
整数零;
//向用户解释程序将执行的操作
printf(“此程序将从0计数到您选择的数字。\n\n”);
//要求用户输入一个值
printf(“请输入一个数字(介于1和10之间):\n”);
scanf(“%d”和&num);
//如果用户选择了正确的范围
while(num>=1&&num将两个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;);
}
num=1;
while(num>0)
{
//要求用户输入一个值
printf(“请输入一个数字(介于1和10之间):\n”);
scanf(“%d”和&num);
//如果用户选择了正确的范围
如果(num>=1&&num
您需要摆脱第一个while循环,并将第二个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;
}
#包括
内部主(空)
{
//变数
int-num;
整数零;
//向用户解释程序将执行的操作
printf(“此程序将从0计数到您选择的数字。\n\n”);
//要求用户输入一个值
printf(“请输入一个数字(介于1和10之间):\n”);
scanf(“%d”和&num);
//如果输入了超出可接受范围的值
而(num<1 | | num>10)
{
printf(“对不起,这是不正确的。\n”);
printf(“请输入一个数字(介于1和10之间):\n”);
scanf(“%d”和&num);
}
因为(0=0;0非常简单
您可以在while循环中使用中断:
#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;
}
#包括
内部主(空)
{
//变数
int-num;
整数零;
//向用户解释程序将执行的操作
printf(“此程序将从0计数到您选择的数字。\n\n”);
//要求用户输入一个值
printf(“请输入一个数字(介于1和10之间):\n”);
scanf(“%d”和&num);
//如果用户选择了正确的范围
while(num>=1&&num您需要显示您的程序。欢迎使用StackOverflow。基于对问题的一些模糊描述,我们无法帮助您使用看不到的代码。请访问,然后返回此处并回答您的问题,并提供相关代码的SSCCE(不是一个大的代码转储),一个问题的清晰描述,并根据该代码提出一个特定的问题,也许我们可以帮助您解决问题。提供一些示例代码。它不需要工作,但需要付出努力。您需要声明几个变量,打印提示,接受输入,将其转换为数字,然后循环该数字。尝试一下。Yes可以工作,但我的导师要求使用两种不同类型的循环。因此,如果我将“while”循环替换为“if”语句,我将把另一种循环放在哪里?请注意while(num>0)外循环和for(…)内循环。完成。您使用的while循环正在破坏内容。如果要添加无关的while循环,请将其添加到任意位置:while(0);不能使用if语句而不是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 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;
}
#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;
}