C:我的循环没有';好像不行
我是C语言的新手,今天我遇到了一个我还没有解决的问题,所以我需要一些帮助。我们被赋予了这个任务: 编写一个程序,演示使用“预读”技术的“while”循环:它要求用户输入介于(包括)10和100之间的数字,输入零终止循环。如果输入的数字小于10或大于100,则会显示错误消息(例如,“错误:不允许250”)。循环结束后,程序打印输入的数字量 我遇到的问题是,一旦我输入了一个有效的数字(10-100之间),程序就会静止不动,不会终止,也不会循环。另一方面,如果我输入一个无效的数字,如8102,它将循环printf(“错误,%d不允许”,num) 代码如下:C:我的循环没有';好像不行,c,loops,while-loop,C,Loops,While Loop,我是C语言的新手,今天我遇到了一个我还没有解决的问题,所以我需要一些帮助。我们被赋予了这个任务: 编写一个程序,演示使用“预读”技术的“while”循环:它要求用户输入介于(包括)10和100之间的数字,输入零终止循环。如果输入的数字小于10或大于100,则会显示错误消息(例如,“错误:不允许250”)。循环结束后,程序打印输入的数字量 我遇到的问题是,一旦我输入了一个有效的数字(10-100之间),程序就会静止不动,不会终止,也不会循环。另一方面,如果我输入一个无效的数字,如8102,它将循环
#include <stdio.h>
main(void)
{
int num;
int counter=1;
printf("Please type in your number between (and including) 10-100\n");
scanf("%d", &num);
if((num <10) || (num >100))
{
printf("Error, %d is not allowed\n", num);
}
while (num > 0)
{
counter++;
if((num <10) || (num >100))
{
printf("Error, %d is not allowed\n", num);
}
counter++;
}
printf("%d numbers were entered!\n", counter);
}
#包括
主(空)
{
int-num;
int计数器=1;
printf(“请键入介于(包括)10-100之间的号码”);
scanf(“%d”和&num);
如果((数字100))
{
printf(“错误%d不允许\n”,num);
}
while(num>0)
{
计数器++;
如果((数字100))
{
printf(“错误%d不允许\n”,num);
}
计数器++;
}
printf(“输入了%d个数字!\n”,计数器);
}
您必须在循环中输入一个数字:
printf("Please type in your number between (and including) 10-100\n");
scanf("%d", &num);
while (num != 0)
{
printf("Please type in your number between (and including) 10-100\n");
scanf("%d", &num);
if((num <10) || (num >100))
{
printf("Error, %d is not allowed\n", num);
}
counter++;
}
请检查scanf
中的返回代码以检测错误
最后,在循环内将计数器增加两次。问题在于,您应该在循环内读取:
printf("Please type in your number between (and including) 10-100\n");
scanf("%d", &num);
while (num != 0)
{
printf("Please type in your number between (and including) 10-100\n");
scanf("%d", &num);
if((num <10) || (num >100))
{
printf("Error, %d is not allowed\n", num);
}
counter++;
}
while(num!=0)
{
printf(“请键入介于(包括)10-100之间的号码”);
scanf(“%d”和&num);
如果((数字100))
{
printf(“错误%d不允许\n”,num);
}
计数器++;
}
您也不需要将计数器增加2倍
请注意,此代码段还将递增0的计数器。如果不需要,则除0之外的数字数为计数器-1。您的while循环被捕获在无限循环中。您将条件设置为“WHILE(num>0)”,但实际上从未更改它。这就是当num在边界内时代码被捕获的原因。这是因为必须从循环内的输入(scanf)中获取数字。否则,循环将永远循环(如果输入的第一个数字>0)。如果无效,您将看到输出。
\include
#include <stdio.h>
void main(void)
{
int num = 0, counter = 0;
printf("Please type in your number between (and including) 10-100\n");
do
{
printf("? ");
scanf("%d", &num);
if (!num)
break;
else if((num < 10) || (num > 100))
printf("Error, %d is not allowed\n", num);
counter++;
} while (num);
printf("%d numbers were entered!\n", counter);
}
真空总管(真空)
{
int num=0,计数器=0;
printf(“请键入介于(包括)10-100之间的号码”);
做
{
printf(“?”);
scanf(“%d”和&num);
如果(!num)
打破
如果((数值<10)| |(数值>100))
printf(“错误%d不允许\n”,num);
计数器++;
}while(num);
printf(“输入了%d个数字!\n”,计数器);
}
已经共享的答案是正确的,您需要在循环中请求输入。此外,您需要为用户提供一种退出循环的方式(例如sentinel),如果您知道输入将是非负的,那么您可以将变量定义为“unsigned int”,而不仅仅是“int”。请参见下面的方法
#include <stdio.h>
int main(void)
{
unsigned int num = 0, counter = 0;
while (num != -1) // need to control with a sentinel to be able to exit
{
printf("Please type in your number between (and including) 10-100. Enter \"-1\" to exit.\n");
scanf("%d", &num);
counter++; // increase the count by one each successful loop
// set the non-allowed numbers
if((num <10) || (num >100))
{
printf("Error, %d is not allowed\n", num);
counter--; // Need to subtract 1 from the counter if enter a non-allowed number, else it will count them
}
} // end while loop
printf("%d numbers were entered!\n", counter); // Show the use how many correct numbers they entered
} // end main
#包括
内部主(空)
{
无符号整数num=0,计数器=0;
while(num!=-1)//需要使用哨兵控制才能退出
{
printf(“请键入介于(包括)10-100之间的号码。输入\“-1\”退出。\n”);
scanf(“%d”和&num);
计数器+++;//每个成功循环增加一个计数
//设置不允许的数字
如果((数字100))
{
printf(“错误%d不允许\n”,num);
计数器--;//如果输入一个不允许的数字,则需要从计数器中减去1,否则将对其进行计数
}
}//在循环结束时结束
printf(“%d个数字已输入!\n”,计数器);//显示输入的正确数字的数量
}//末端总管
您需要扫描循环的每次迭代(…),以获取新的编号。