在加仑<100和小时<24的情况下,如何使循环显示在执行后结束。我能够得到“鱼在……小时后死亡”,但不确定如何停止其中的第一个printf()。从执行循环。
我尝试使用if语句中缩进的break语句,但这仅会影响for循环。
#include <stdio.h>
int main()
{
double add, vol = 500;
int hour;
printf("Please enter additional water added per hour: ");
scanf("%lf", &add);
for (hour = 1; hour <= 24; hour++)
{
vol = (vol * 0.90) + add;
printf("The volume is %.2f gallons after %d hours.\n", vol, hour);
if (hour <= 23 && vol < 100)
printf("The fish died after %d hours.", hour);
else if (hour == 24 && vol >= 100)
printf("Alas, The fish who lived.");
}
return 0;
}
预期结果:
Please enter additional water added per hour: 6
The volume is 456.00 gallons after 1 hours.
The volume is 416.40 gallons after 2 hours.
The volume is 380.76 gallons after 3 hours.
...
The volume is 103.33 gallons after 22 hours.
The volume is 99.00 gallons after 23 hours.
The fish died after 23 hours.
实际结果:
Please enter additional water added per hour: 6
The volume is 456.00 gallons after 1 hours.
The volume is 416.40 gallons after 2 hours.
The volume is 380.76 gallons after 3 hours.
...
The volume is 103.33 gallons after 22 hours.
The volume is 99.00 gallons after 23 hours.
The fish died after 23 hours. The volume is 95.10 gallons after 24 hours.
答案 0 :(得分:1)
C不是Python,缩进在语法上并不重要。复合语句必须包含在{..}
中:
if (hour <= 23 && vol < 100)
{
printf("The fish died after %d hours.", hour);
break ;
}
else if (hour == 24 && vol >= 100)
{
printf("Alas, The fish who lived.");
}
我建议您始终将{..}
用于条件块或循环块,即使是单个语句也是如此。它使维护更加简单。
但是,break;
可以说是退出循环(与continue
一起使用)的一种相当不雅且结构不良的方式。更好的结构化解决方案是仅通过循环约束来终止循环。在这种情况下,可以通过以下方式完成:
for( hour = 1;
vol > 100 && hour <= 24;
hour++)
有额外测试的开销,但是比这更复杂的代码,可能带有多个break
,可能会变得难以维护,调试和理解。
答案 1 :(得分:0)
弄清楚您的循环控制您仍然有一些问题。如果在输入过程中发生匹配失败,您还会邀请无尽循环,因为您无法检查scanf
的返回值(可能是新的最大陷阱之一) C程序员)。尝试一下。在提示符下输入"one gallon"
进行输入,然后看看会发生什么。此外,检查退货后,您必须处理3种情况:
scanf
。这意味着您负责每次检查scanf
的退货。
(return == EOF)
用户通过按 Ctrl + d (或在Windows Ctrl + z 上)生成手册EOF
来取消输入。 CTRL+Z does not generate EOF in Windows 10 (early versions)); (return < expected No. of conversions)
发生匹配或 input 失败。对于 matching 失败,您必须考虑输入缓冲区中剩余的每个字符。 (在输入缓冲区中向前扫描以读取和丢弃字符,直到找到'\n'
或EOF
为止);最后(return == expected No. of conversions)
表示读取成功-然后由您检查输入是否满足任何其他条件(例如,正整数,正浮点,在所需范围内,等等。)。 / li>
进入循环逻辑。给定您的示例和问题“在for循环内创建if语句”,我想我知道您想去的地方,并且可以使用最简单,使用最少的工具和坦白地说,嵌套循环控制的强制表达式。好的ole goto
语句。它在编程中仍然占有一席之地,虽然不应过度使用它,但在某些情况下(例如上述情况),实际上在您遇到的情况下,可以使用goto
跳出循环并向下控制几行。 (限制为longjmp
-您不想使用它跳出函数)
例如,如果我了解您在鱼类生存中的目标,则可以整理逻辑,并通过类似于以下内容的方式将scanf
验证组合在一起:
#include <stdio.h>
int main (void) {
double add, vol = 500;
int hour;
for (;;) { /* loop continually until valid input is received */
int rtn; /* varible for scanf return - EVERY TIME */
/* there is no conversion taking place, fputs will do */
fputs ("Please enter additional water added per hour: ", stdout);
if ((rtn = scanf ("%lf", &add)) == EOF) { /* handle EOF case */
fputs ("(user canceled input)\n", stderr);
return 1;
}
else if (rtn == 0) { /* handle matching failure */
fputs ("error: invalid double input.\n", stderr);
/* empty all characters from stdin before next input */
for (int c = getchar(); c != '\n' && c != EOF; c = getchar()) {}
}
else /* handle good input */
break;
}
for (hour = 0; hour < 24; hour++) { /* loop 0 < 24 */
vol = (vol * 0.90) + add; /* adjust output hour + 1 below */
printf ("The volume is %.2f gallons after %d hours.\n", vol, hour+1);
if (vol < 100) { /* don't forget the '\n' */
printf ("The fish died after %d hours.\n", hour + 1);
goto deadfish; /* jump to deadfish label */
}
}
puts ("Alas, The fish who lived."); /* again, no conversion */
deadfish:; /* good ole goto label */
return 0;
}
(注意:通常,C中的循环将运行0 < n
,您可以按照上面的要求调整输出。虽然这不是强制性的,但按照惯例,这非常有用,并且可以容纳数组C中的零索引,以及运行0 -> 23
而非1 - > 24
的时钟上的小时数。电影《零黑暗的三十岁》的标题就是一个很好的比喻(例如,零时30分钟或12:30 am
。)您无法到达1 <= 24
,因此这些限制无法正确地模拟时间概念)
关于使用goto
的注意事项。它与vol < 100
检查一起使用。如果vol < 100
测试TRUE
,则控制权将传递到与goto
语句关联的 label 。 (此处的标签为deadfish:;
)。这提供了几个好处。您的for
循环可以简单地跟踪时间-储罐泄漏到24
的小时数。请注意,音量降到100
以下如何使控制跳到在正常循环出口执行的活鱼输出。
虽然您可以通过合并if (vol < 100) /* print dead fish */; break; else if ...
来获得所需的死鱼输出,但是以这种方式在常规循环退出时获取/* print live fish */
方法变得非常困难,而无需再次检查循环计数器else if
子句中的内容。 goto
为解决该问题提供了一种优雅的解决方案,它允许您在音量低于限制时通过对/* print live fish */
的控制,同时保留正常的循环出口而无需测试您的循环计数器变量第二次进入循环本身。
使用/输出示例
确保您的程序可以处理每次用户输入时踩在键盘上的猫。您每次都必须验证,例如
$ ./bin/deadfish
Please enter additional water added per hour: one gallon
error: invalid double input.
Please enter additional water added per hour: 1
The volume is 451.00 gallons after 1 hours.
The volume is 406.90 gallons after 2 hours.
The volume is 367.21 gallons after 3 hours.
The volume is 331.49 gallons after 4 hours.
The volume is 299.34 gallons after 5 hours.
The volume is 270.41 gallons after 6 hours.
The volume is 244.37 gallons after 7 hours.
The volume is 220.93 gallons after 8 hours.
The volume is 199.84 gallons after 9 hours.
The volume is 180.85 gallons after 10 hours.
The volume is 163.77 gallons after 11 hours.
The volume is 148.39 gallons after 12 hours.
The volume is 134.55 gallons after 13 hours.
The volume is 122.10 gallons after 14 hours.
The volume is 110.89 gallons after 15 hours.
The volume is 100.80 gallons after 16 hours.
The volume is 91.72 gallons after 17 hours.
The fish died after 17 hours.
(通过键盘测试的猫已通过...)
最接近100加仑的死鱼:
$ ./bin/deadfish
Please enter additional water added per hour: 6.53
The volume is 456.53 gallons after 1 hours.
The volume is 417.41 gallons after 2 hours.
The volume is 382.20 gallons after 3 hours.
The volume is 350.51 gallons after 4 hours.
The volume is 321.99 gallons after 5 hours.
The volume is 296.32 gallons after 6 hours.
The volume is 273.22 gallons after 7 hours.
The volume is 252.42 gallons after 8 hours.
The volume is 233.71 gallons after 9 hours.
The volume is 216.87 gallons after 10 hours.
The volume is 201.71 gallons after 11 hours.
The volume is 188.07 gallons after 12 hours.
The volume is 175.79 gallons after 13 hours.
The volume is 164.75 gallons after 14 hours.
The volume is 154.80 gallons after 15 hours.
The volume is 145.85 gallons after 16 hours.
The volume is 137.80 gallons after 17 hours.
The volume is 130.55 gallons after 18 hours.
The volume is 124.02 gallons after 19 hours.
The volume is 118.15 gallons after 20 hours.
The volume is 112.86 gallons after 21 hours.
The volume is 108.11 gallons after 22 hours.
The volume is 103.83 gallons after 23 hours.
The volume is 99.97 gallons after 24 hours.
The fish died after 24 hours.
(每加仑3-100死)
第一条活鱼:
$ ./bin/deadfish
Please enter additional water added per hour: 6.54
The volume is 456.54 gallons after 1 hours.
The volume is 417.43 gallons after 2 hours.
The volume is 382.22 gallons after 3 hours.
The volume is 350.54 gallons after 4 hours.
The volume is 322.03 gallons after 5 hours.
The volume is 296.36 gallons after 6 hours.
The volume is 273.27 gallons after 7 hours.
The volume is 252.48 gallons after 8 hours.
The volume is 233.77 gallons after 9 hours.
The volume is 216.94 gallons after 10 hours.
The volume is 201.78 gallons after 11 hours.
The volume is 188.14 gallons after 12 hours.
The volume is 175.87 gallons after 13 hours.
The volume is 164.82 gallons after 14 hours.
The volume is 154.88 gallons after 15 hours.
The volume is 145.93 gallons after 16 hours.
The volume is 137.88 gallons after 17 hours.
The volume is 130.63 gallons after 18 hours.
The volume is 124.11 gallons after 19 hours.
The volume is 118.24 gallons after 20 hours.
The volume is 112.95 gallons after 21 hours.
The volume is 108.20 gallons after 22 hours.
The volume is 103.92 gallons after 23 hours.
The volume is 100.07 gallons after 24 hours.
Alas, The fish who lived.
(节省了7-100加仑-如果需要的话,我会将其二等分到1000加仑)
仔细检查一下,如果还有其他问题,请告诉我。将goto
放入您的C工具箱中,知道它不会被过度使用,但是它有一些用途,可以提供最佳解决方案,并且在打破嵌套循环的情况下,它是您唯一的工具可以做到的工具箱。