我正在尝试制作一个程序,询问您练习了多少天,然后每天询问您制作了多少航班,然后确定每天的航班平均值,这是我现在的代码:
#include <stdio.h>
int main() {
int days, flights, i;
double length, total, average;
printf("How many days have you been practicing?\n");
scanf("%d", &days);
for(i=1; i<=days; i++) {
printf("How many flights were completed in day #%d?\n", i);
scanf("%d", &flights);
for(i=1; i<=flights; i++){
printf("How long was flight #%d?\n", i++);
scanf("%lf", &length);
length += total;
average = total / flights;
printf("Day #%d: The average distance is %.3lf\n", i, average);
}
}
return 0;
}
现在示例输出如下所示:
你练了几天?
2
第1天完成了多少次航班?
2
1号航班有多长时间?
5.00
第3天:平均距离为0.000。
第2天完成了多少次航班?
3
1号航班有多长时间?
7.50
3号航班有多长时间?
13.00
第5天:平均距离为0.000。
正如您所看到的,现在输出存在多个问题,平均距离没有计算,我无法为每个航班输入数字,而且航班和日期#是不正确的。任何帮助将不胜感激,因为我是新手!举个例子,最终输出应该是这样的:
你练了几天?
2
第1天完成了多少次航班?
2
1号航班有多长时间?
5.00
2号航班有多长时间?
10.00
第1天:平均距离是7.500。
第2天完成了多少次航班?
3
1号航班有多长时间?
7.50
2号航班有多长时间?
13.00
3号航班有多长时间?
15.75
第2天:平均距离为12.083。
答案 0 :(得分:2)
有几个问题,我为每个修复程序留下了评论。如果您有疑问,请告诉我
#include <stdio.h>
int main() {
int days, flights, i, j;
double length, total, average;
printf("How many days have you been practicing?\n");
scanf("%d", &days);
for (i = 1; i <= days; i++) {
printf("How many flights were completed in day #%d?\n", i);
scanf("%d", &flights);
// Reset total
total = 0.0;
// Use a different index variable than i, I used j
for (j = 1; j <= flights; j++) {
// Do not increment i here (I removed i++)
printf("How long was flight #%d?\n", j);
scanf("%lf", &length);
// This was probably backwards, you never assigned total
//length += total;
total += length;
}
// Compute the average per day outside the flights loop
average = total / flights;
printf("Day #%d: The average distance is %.3lf\n", i, average);
}
return 0;
}
答案 1 :(得分:0)
为内部for循环使用不同的循环计数器。例如。使用j
代替i
。
for(int j=1; j<=flights; j++){
您还需要从内循环移动以下行。
average = total / flights;
printf("Day #%d: The average distance is %.3lf\n", i, average);
它们应该在内循环之后。
答案 2 :(得分:0)
请注意,您已为两个循环使用了相同的迭代变量:
for(i=1; i<=days; i++) {
printf("How many flights were completed in day #%d?\n", i);
scanf("%d", &flights);
for(i=1; i<=flights; i++){
...
使用另一个变量j
:
for(i=1; i<=days; i++) {
printf("How many flights were completed in day #%d?\n", i);
scanf("%d", &flights);
for(int j=1; j<=flights; j++){
...
此外,您需要按如下方式更正业务逻辑:
for(i=1; i<=days; i++) {
total = 0.0;
printf("How many flights were completed in day #%d?\n", i);
scanf("%d", &flights);
for(j=1; j<=flights; j++){
printf("How long was flight #%d?\n", j);
scanf("%lf", &length);
total += length;
}
average = total / flights;
printf("Day #%d: The average distance is %.3lf\n", i, average);
}
答案 3 :(得分:0)
我修复了你的代码。你不能在嵌套的#34;中使用相同的变量来实现&#34;循环,因为第二次使用变量会混淆第一次使用同一个变量。出于这个原因,我添加了一个名为J的计数器变量。
#include <stdio.h>
int main() {
int days, flights, i,j;
double length, total, average;
printf("How many days have you been practicing?\n");
scanf("%d", &days);
for(i=1; i<=days; i++) {
printf("How many flights were completed in day #%d?\n", i);
scanf("%d", &flights);
for(j=1; j<=flights; j++){
printf("How long was flight #%d?\n", i++);
scanf("%lf", &length);
total += length;
}
average = total / flights;
printf("Day #%d: The average distance is %.3lf\n", i, average);
}
return 0;
}