1
目标是根据统一编号为不同玩家分配平均值。问题在于它不断跳过第二个printf,并且switch语句中的字符不起作用。我敢肯定,这是我的一个非常简单的错误,但我似乎无法找到它。遇到了嵌套的循环问题
int main(){
float ab;
float hits;
int un;
char pa;
printf("Please enter the player number, or -1 to exit. \n");
scanf("%d%*c \n", &un);
while(un!= -1)
{
printf("Please enter either an H for a hit or an O for a out, enter E to stop. \n");
scanf("%c%*c", &pa);
while(pa != 'E')
{
switch (pa)
{
case 'h':
case 'H':
ab += 1;
hits +=1;
break;
case 'o':
case 'O':
ab+=1;
break;
default:
printf("Error: Please insert an O or H \n");
break;
}
float average = (ab/hits);
printf("Player %d's score is equal to: %d \n", un, average);
printf("Please enter the player number, or -1 to exit. \n");
scanf("%d%*c \n", &un);
}
}
return 0;
}
使用未初始化变量的值'ab'和'hits',它具有自动存储时间,调用*未定义行为*。 – MikeCAT
在'printf()'中,将类型为“float”的'average'传递给'%d',调用类型为int的数据,也会调用未定义的行为。 – MikeCAT
你确定第二个'printf()'被跳过吗? (由于未定义的行为,可能会跳过)是不是因为第一个'scanf()正在等待非空白字符而尚未调用? – MikeCAT