程序要求输入速度,当速度高于100时,它不应在平均速度的计算中包括该值。我该怎么办?
我放了一个i--;在else如果它说else if(速度> 100)。它重复了这个问题,但是并没有删除大于100的值。
#include <stdio.h>
int main(){
int i;
double speed, sum = 0.0;
float average;
for( i = 0; i < 10; i ++ ){ // asks 10 times the printf
printf("%d Enter speed: ", i);
scanf("%lf",&speed); // saves the input speed
sum += speed; // sum = sum + speed;
// decides which gear to use
if (speed == 0){
printf("gear N\n");
}else if (speed < 0 ){
printf("gear R\n");
}else if(speed <= 10.0){
printf("gear 1\n");
}else if (speed <= 30.0){
printf("gear 2\n");
}else if (speed <= 60.0){
printf("gear 3\n");
}else if (speed <= 80.0){
printf("gear 4\n");
}else if (speed <= 100.0){
printf("gear 5\n");
}elseif (speed > 100 ){ // when input higher than 100 dont save the input and ask again
printf("max speed 100 km/h\n");
i--;
}else
printf("Error!\n");
}
average = sum/i; // average calculation
printf("average speed = %.2lf km/h", average); // prints out the average
return(0);
}
当我输入200时,应删除该值并再次询问。当我输入200时得到的结果再次询问,但使用200计算平均值。
答案 0 :(得分:0)
也许这可以上班:
elseif (speed > 100 ){ // when input higher than 100 dont save the input and ask again
printf("max speed 100 km/h\n");
sum-=speed;
i--;
}