我第一次尝试用 C 编程,并将其应用于一些具体的东西......
我正在用这个问题创建的程序处理一个while循环。该计划的目标是计算一组卡车每加仑的平均英里数。我希望它在输入 -1 作为消耗的加仑数后立即终止,但我必须输入两次,一次用于加仑数,一次用于英里数。我发现这个输入实际上被用作结果计算的一部分。这是代码:
#include <stdio.h>
int main()
{
int tanks, miles;
float gallons = 0, average = 0, miles_per_gallon = 0;
tanks = 0;
while (gallons != -1) {
tanks += 1;
miles_per_gallon = (float)miles / gallons;
average = average + miles_per_gallon;
printf("The miles / gallon for this tank was %.3fn",
miles_per_gallon);
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
printf("Enter the miles driven: ");
scanf("%d", &miles);
}
average /= tanks;
printf("The overall average miles/gallon was %.3f", average);
return 0;
}
下面是一些示例输出:
C:>gallons
Enter the gallons used (-1 to end): 12.3
Enter the miles driven: 700
The miles / gallon for this tank was 56.911
Enter the gallons used (-1 to end): 13.4
Enter the miles driven: 666
The miles / gallon for this tank was 49.701
Enter the gallons used (-1 to end): 17.3
Enter the miles driven: 644
The miles / gallon for this tank was 37.225
Enter the gallons used (-1 to end): 15.5
Enter the miles driven: 777
The miles / gallon for this tank was 50.129
Enter the gallons used (-1 to end): -1
Enter the miles driven: -1
The miles / gallon for this tank was 1.000
The overall average miles/gallon was 38.993
感谢您的任何帮助。
问题在于,代码中的语句序列使得直到请求第二个输入后才能达到对循环退出条件的检查。您可以在输入-1
后立即添加检查,然后从循环中脱离出来。或者,您可以要求在加仑之前输入英里数。
for (;;) { /* This is a "forwver" loop; we break out from the middle */
tanks += 1;
miles_per_gallon = (float)miles / gallons;
average = average + miles_per_gallon;
printf("The miles / gallon for this tank was %.3fn",
miles_per_gallon);
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
/* This is where you break from the loop: */
if (gallons == -1) return 0;
printf("Enter the miles driven: ");
scanf("%d", &miles);
}
好吧,您应该能够自己解决它,如果您只是更改循环或在加仑输入后放置 if 语句,这很容易
while (gallons != -1) {
tanks += 1;
miles_per_gallon = ( float ) miles / gallons;
average = average + miles_per_gallon;
printf("The miles / gallon for this tank was %.3fn", miles_per_gallon);
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
if(gallons==-1){
printf("Program terminated");
return 0;
}
printf("Enter the miles driven: ");
scanf("%d", &miles);
}
读取加仑后检查退出条件。我已经对你的代码进行了一些更改---因为你只是在读取加仑后才中断,我已将 while 条件更改为 true。其次,我将您的测试更改为 <=0,如果输入 0 除以 0
,这将破坏您的数学,任何小于 0 的东西都没有意义。第三,我将计算和报告更改为读取值后,因此您不会除以零
while (1) {
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
if(gallons <= 0) break;
printf("Enter the miles driven: ");
scanf("%d", &miles);
tanks += 1;
miles_per_gallon = ( float ) miles / gallons;
average = average + miles_per_gallon;
printf("The miles / gallon for this tank was %.3fn", miles_per_gallon);
}
while (gallons != -1) {
/* snip */
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
printf("Enter the miles driven: ");
scanf("%d", &miles);
}
请注意,只有在提出这两个问题后,您才会评估退出条件。如果你想避免要求里程,你必须把你的循环扭曲得更远一点。它看起来像这样:
while (gallons != -1) {
/* snip */
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
if (gallons == -1)
break; /* exit the while loop */
printf("Enter the miles driven: ");
scanf("%d", &miles);
}
当然,像这样退出中间的循环在边缘有点粗糙,但我没有立即看到更方便的方法在不问第二个问题的情况下终止循环。