C程序。我正在尝试进行多项选择测验。我已经使用for循环从问题1运行到2到3等。但是在运行程序之后它永远不会退出for循环,即使我只指定了count< = 3。为什么?你能看出我做错了吗?
#include <stdio.h>
int main (void)
{
char s;
char response[]={'a','b','c','d'};
int ,x;
int count,result;
printf (" Listo para empezar?\n\n Please type 's' for si or 'n' for no.\n");
scanf ("%c",&s);
if (s='s'){
printf ("Bueno. Ya Empezamos!\n");
}
for (count=0;count<=3;count++){
printf ("1.Que significa la palabra 'conocer'\n");
printf ("1.\t to like 2.\t to know\t 3. to do\t 4. to eat\n");
scanf ("%d",&x);
if (x=response[1])
{
printf ("Equivocado!\n 'conocer'significa: to know\n");
}
else {
printf ("Correcto!\n");
}
printf ("1.Que significa la palabra 'ser'\n");
printf ("1.\t to be 2.\t to return\t 3. to be (location)\t 4. to eat\n");
scanf ("%d",&x);
if (x=response[0])
{
printf ("Equivocado!\n 'ser'significa: to be\n");
}
else {
printf ("Correcto!\n");
}
printf ("1.Que significa la palabra 'poder'\n");
printf ("1.\tto be able to 2.\t to run\t 3. to play\t 4. to put\n");
scanf ("%d",&x);
if (x=response[0])
{
printf ("Equivocado!\n 'poder'significa: to be able to\n");
}
else {
printf ("Correcto!\n");
}
result=(4/count)*100;
printf ("%d is your percentage out of %d attempts\n",result,count);
}
return 0;
}
答案 0 :(得分:1)
int x
的声明是错误的;标识符旁边有一个逗号。==
,不是 =
。 此是分配运算符。int / int
int
不是float
或double
或long double
,因此始终为0
。此外,float
打印出"%f"
格式字符串。答案 1 :(得分:0)
if (x=response[0])
应该是
if (x==response[0])
应在所有if()
s
请注意,您正在将字符与整数进行比较。另外
result=(4/count)*100;
有0分割操作。
整数除零导致未定义的行为