Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 程序不遵循while语句_C_Loops - Fatal编程技术网

C 程序不遵循while语句

C 程序不遵循while语句,c,loops,C,Loops,使用visual studio 2015“C” 我要求用户输入一个介于3-40之间的数字,如果用户没有输入正确的值,它将输入while语句,并不断循环并再次询问,直到他们输入一个有效值,但是不管怎样,它总是进入while循环,并且只循环一次 #include <stdio.h> int main(void) { int counter; int marks; printf("---=== IPC mark Analyser ===---\n"); printf("Pleas

使用visual studio 2015“C” 我要求用户输入一个介于3-40之间的数字,如果用户没有输入正确的值,它将输入while语句,并不断循环并再次询问,直到他们输入一个有效值,但是不管怎样,它总是进入while循环,并且只循环一次

#include <stdio.h>

int main(void)
{
int counter;
int marks;



printf("---=== IPC mark Analyser ===---\n");

printf("Please enter the number of marks(between 3 and 40): ");
scanf("%d", &counter);

while (counter >40 || counter <3);
{
    printf("Invalid number, enter a number between 3 and 40 inclusive: ");
    scanf("%d", &counter);
} 


    printf("");

}
#包括
内部主(空)
{
整数计数器;
整数标记;
printf(“--==IPC标记分析器===--\n”);
printf(“请输入标记数(介于3和40之间):”;
scanf(“%d”和计数器);

while(计数器>40 | |计数器<代码>while(计数器>40 | |计数器40 | | |计数器问题在后面);“在您的while行:)此外,最好的编码实践是将变量设置为零,以删除内存中可能存在的垃圾数据

问题在于while循环后的分号,因此您的程序似乎是一条语句

下面给出了正确的代码

#include <stdio.h>

int main(void)
{
int counter;
int marks;



printf("---=== IPC mark Analyser ===---\n");

printf("Please enter the number of marks(between 3 and 40): ");
scanf("%d", &counter);

while (counter >40 || counter <3)
{
    printf("Invalid number, enter a number between 3 and 40 inclusive: ");
    scanf("%d", &counter);
}


    printf("");

}
#包括
内部主(空)
{
整数计数器;
整数标记;
printf(“--==IPC标记分析器===--\n”);
printf(“请输入标记数(介于3和40之间):”;
scanf(“%d”和计数器);

虽然(counter>40 | | counter my bad不知道我添加了它学习如何在Visual Studio中使用内置调试器。它非常好。有了它,你可以在监视变量及其值时逐行检查代码。好的,但是你注意到代码本身有什么问题吗谢谢,它现在可以工作了。我可以问一下为什么分号会导致这个问题吗m?语法允许循环体是单个表达式(没有
{}
),甚至是空表达式。您可以使用
{}
添加另一个作用域,例如控制变量的生存期,这就是为什么第二个
scanf
始终执行,无论您第一次输入什么。感谢您的解释:)删除未使用的变量绝对是最佳实践。当变量由输入语句设置时,将其归零没有什么特别的好处,尽管代码应该在使用值之前检查输入是否成功。
while (counter >40 || counter <3)
#include <stdio.h>

int main(void)
{
int counter;
int marks;



printf("---=== IPC mark Analyser ===---\n");

printf("Please enter the number of marks(between 3 and 40): ");
scanf("%d", &counter);

while (counter >40 || counter <3)
{
    printf("Invalid number, enter a number between 3 and 40 inclusive: ");
    scanf("%d", &counter);
}


    printf("");

}