C语言 预期的 ; 之前) C 中的令牌错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25876405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expected ; before ) token error in C
提问by jlest
I'm trying to figure out a homework assignment in C. I'm supposed to have the user enter integers until they enter a negative number. At that point the program needs to stop inputting and proceed to output the sum, number of tries before a negative number is entered, and the mean.
我试图找出 C 中的家庭作业。我应该让用户输入整数,直到他们输入负数。此时程序需要停止输入并继续输出总和、输入负数之前的尝试次数以及平均值。
I can't seem to find any errors in my code (although I'm sure there is), but when I try to compile I get multiple errors on my output printfstatements that say both expected ';' before ')' tokenand expected statement before ')' token. I must be blind. Please enlighten me.
我似乎在我的代码中找不到任何错误(尽管我确定有),但是当我尝试编译时,我的输出printf语句中出现多个错误,同时显示expected ';' before ')' token和expected statement before ')' token。我一定是瞎了。请赐教。
Here's all my code thus far:
到目前为止,这是我的所有代码:
int main(void)
{
int i=0,sum=0,tries=0;
int mean=sum/tries;
do
{
printf("Please enter a number %i. When finished, enter "
"a negative number. \n",i);
scanf("%i",&i);
sum+=i;
tries++;
}
while(i>=-1);
if((sum<=0) && (i<=-1))
{
printf("No valid numbers were entered. Try again. ");
}
else
{
printf("Sum is %i\n"),sum);
printf("%i tries \n"),tries);
printf("Mean is %i \n"),mean);
}
return 0;
}
回答by Kosovan
You have too many parentheses
你的括号太多了
printf("Sum is %i\n"),sum);
printf("%i tries \n"),tries);
printf("Mean is %i \n"),mean);
Should be
应该
printf("Sum is %i\n",sum);
printf("%i tries \n",tries);
printf("Mean is %i \n",mean);
Full code:
完整代码:
int main(void)
{
int i=0,sum=0,tries=0;
int mean=sum/tries;
do
{
printf("Please enter a number %i. When finished, enter "
"a negative number. \n",i);
scanf("%i",&i);
sum+=i;
tries++;
}
while(i>=-1);
if((sum<=0) && (i<=-1))
{
printf("No valid numbers were entered. Try again. ");
}
else
{
printf("Sum is %i\n",sum);
printf("%i tries \n",tries);
printf("Mean is %i \n",mean);
}
return 0;
}
回答by NetVipeC
- You forgot the
includeline (#include <stdio.h>). - And miss type close parenthesisin the print lines, should be
printf("Sum is %i\n", sum);and notprintf("Sum is %i\n"),sum);should also give some error similar to the posted.
- 您忘记了
include(#include <stdio.h>)行。 - 并且在打印行中缺少类型的右括号,应该
printf("Sum is %i\n", sum);和不printf("Sum is %i\n"),sum);应该也给出一些类似于贴出的错误。
This would be the fixed code:
这将是固定代码:
#include <stdio.h>
int main(void) {
int i = 0, sum = 0, tries = 0;
int mean = sum / tries;
do {
printf("Please enter a number %i. When finished, enter "
"a negative number. \n",
i);
scanf("%i", &i);
sum += i;
tries++;
} while (i >= -1);
if ((sum <= 0) && (i <= -1)) {
printf("No valid numbers were entered. Try again. ");
} else {
printf("Sum is %i\n", sum);
printf("%i tries \n", tries);
printf("Mean is %i \n", mean);
}
return 0;
}

