【发布时间】:2019-10-16 17:29:32
【问题描述】:
可以将 for 循环中的 while 循环视为嵌套的 while 循环吗?
我如何使用 do while 来解决同样的问题?
#include <stdio.h>
#define NUMS 3
int main() {
// insert code here...
int highVal;
int lowVal;
int i;
printf("---=== IPC Temperature Analyzer ===---\n");
for (i=1;i<=NUMS;i++){
printf("Enter the high value for day %d: ",i);
scanf("%d",&highVal);
printf("Enter the low value for day %d: ",i);
scanf("%d",&lowVal);
while((highVal < lowVal)||(highVal >= 41 || lowVal <= -41)){
printf("Incorrect values,temperature must be in the range -40 to 40,high must be greater than low\n");
i--;
highVal=1;
lowVal=0;
}
}
return 0;
}
我得到了我需要得到的结果,但是分配要求使用嵌套的 while 循环(或 do while)以及 for 循环来提示用户。
【问题讨论】:
-
“嵌套 while 循环”不是一个技术术语或精确术语,因此您需要询问您的老师/教授什么算数和不算数。
-
您的“嵌套 while 循环”不是嵌套 while 循环,它是一个写得很糟糕的
if。你应该有scanfs inside 内部循环。 -
这绝对是一个嵌套循环。现在是嵌套的while循环吗?好吧,while 或 for 循环并不是真正的事情。它们只是制作循环的不同方式。您可以交替使用 while 循环和 for 循环
标签: c loops while-loop nested