C: 'else' if 条件满足时执行 - 结构
C: 'else' being executed when if condition is satisfied - structures
我目前正在从一本书上学习 C,一个问题是编写一个程序来查找用户输入 2 次之间经过的时间,我已经使用 24 小时格式完成了。
问题是 main 函数中的 while 循环没有在应该执行的时候执行。即用户输入的时间超出范围(小于 0、大于 24 小时等)
while(error == 1) {
printf("Enter time 1 in the format hours:mins:sec\n");
scanf("%i:%i:%i", &time1.hours, &time1.minutes, &time1.seconds);
while 循环应该继续执行直到用户输入有效时间,但它只是被跳过。
我想原因可能是因为检查时间并将全局变量'error'赋值等于1或0的函数总是执行else语句(设置error = 0),即使如果陈述为真。
void checkTime (struct time a)
{
if(a.hours < 0 || a.hours > 23) {
printf("Please enter a valid figure for hours in the range 1 to 23\n");
error = 1;
}
if(a.minutes < 0 || a.minutes > 59) {
printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
error = 1;
}
if(a.seconds < 0 || a.seconds > 59) {
printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
error = 1;
}
else {
error = 0;
}
}
这是一个与结构工作方式有关的逻辑问题吗?
完整代码如下:
#include <stdio.h>
//Program to find the time elapsed between 2 times entered by user
int error = 0;
struct time
{
int hours;
int minutes;
int seconds;
};
//Function to find elapsed time between the 2 arguments
struct time elapsed_time (struct time time1, struct time time2)
{
struct time eTime;
if (time2.hours > time1.hours)
eTime.hours = time2.hours - time1.hours;
else
eTime.hours = (24 - time1.hours) + time2.hours;
if (time2.minutes > time1.minutes)
eTime.minutes = time2.minutes - time1.minutes;
else
eTime.minutes = (60 - time1.minutes) + time2.minutes;
if (time2.seconds > time1.seconds)
eTime.seconds = time2.seconds - time1.seconds;
else
eTime.seconds = (60 - time1.seconds) + time2.seconds;
return eTime;
}
//Function to check if inputed times are within valid parameters
void checkTime (struct time a)
{
if(a.hours < 0 || a.hours > 23) {
printf("Please enter a valid figure for hours in the range 1 to 23\n");
error = 1;
}
if(a.minutes < 0 || a.minutes > 59) {
printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
error = 1;
}
if(a.seconds < 0 || a.seconds > 59) {
printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
error = 1;
}
else {
error = 0;
}
}
int main (void)
{
struct time time1;
struct time time2;
struct time main_eTime;
printf("Enter time 1 in the format hours:mins:sec\n");
scanf("%i:%i:%i", &time1.hours, &time1.minutes, &time1.seconds);
checkTime(time1);
while(error == 1) {
printf("Enter time 1 in the format hours:mins:sec\n");
scanf("%i:%i:%i", &time1.hours, &time1.minutes, &time1.seconds);
}
printf("Enter time 2 in the format hours:mins:sec\n");
scanf("%i:%i:%i", &time2.hours, &time2.minutes, &time2.seconds);
checkTime(time2);
while(error == 1) {
printf("Enter time 2 in the format hours:mins:sec\n");
scanf("%i:%i:%i", &time2.hours, &time2.minutes, &time2.seconds);
}
main_eTime = elapsed_time(time1, time2);
printf("Elapsed time between time 1 and time 2 is %i:%i:%i\n", main_eTime.hours,
main_eTime.minutes, main_eTime.seconds);
return 0;
}
我认为您的分支条件有问题,尤其是在 checkTime
函数中。如果秒有效,最后的else
条件将执行,它与您输入的小时和分钟是否正确无关。
改为:
void checkTime (struct time a)
{
if(a.hours < 0 || a.hours > 23) {
printf("Please enter a valid figure for hours in the range 1 to 23\n");
error = 1;
}
else if(a.minutes < 0 || a.minutes > 59) {
printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
error = 1;
}
else if(a.seconds < 0 || a.seconds > 59) {
printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
error = 1;
}
else {
error = 0;
}
}
只有当所有三个变量(小时、分钟和秒都有效)时,以上才会 运行 else
阻塞。
您的代码结构是:
if (COND_1) {
...
}
if (COND_2) {
...
}
if (COND_3) {
...
}
else {
...
}
如果 COND_1
、COND_2
或 COND_3
的 none 为真,您似乎认为 else
应该 运行。但这不是它的工作原理:else
附加到前面的 if
语句,仅此而已。 else
块是否 运行 唯一重要的部分是 COND_3
条件。
你应该做的是:
if (COND_1) {
...
}
else if (COND_2) {
...
}
else if (COND_3) {
...
}
else {
...
}
通过以这种方式构建代码,我们确保只执行一个块。
... 或:
error = 0;
if(a.hours < 0 || a.hours > 23) {
printf("Please enter a valid figure for hours in the range 1 to 23\n");
error = 1;
}
if(a.minutes < 0 || a.minutes > 59) {
printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
error = 1;
}
if(a.seconds < 0 || a.seconds > 59) {
printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
error = 1;
}
通过在开始时设置 error = 0
,您根本不需要 else
块。
我目前正在从一本书上学习 C,一个问题是编写一个程序来查找用户输入 2 次之间经过的时间,我已经使用 24 小时格式完成了。
问题是 main 函数中的 while 循环没有在应该执行的时候执行。即用户输入的时间超出范围(小于 0、大于 24 小时等)
while(error == 1) {
printf("Enter time 1 in the format hours:mins:sec\n");
scanf("%i:%i:%i", &time1.hours, &time1.minutes, &time1.seconds);
while 循环应该继续执行直到用户输入有效时间,但它只是被跳过。
我想原因可能是因为检查时间并将全局变量'error'赋值等于1或0的函数总是执行else语句(设置error = 0),即使如果陈述为真。
void checkTime (struct time a)
{
if(a.hours < 0 || a.hours > 23) {
printf("Please enter a valid figure for hours in the range 1 to 23\n");
error = 1;
}
if(a.minutes < 0 || a.minutes > 59) {
printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
error = 1;
}
if(a.seconds < 0 || a.seconds > 59) {
printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
error = 1;
}
else {
error = 0;
}
}
这是一个与结构工作方式有关的逻辑问题吗?
完整代码如下:
#include <stdio.h>
//Program to find the time elapsed between 2 times entered by user
int error = 0;
struct time
{
int hours;
int minutes;
int seconds;
};
//Function to find elapsed time between the 2 arguments
struct time elapsed_time (struct time time1, struct time time2)
{
struct time eTime;
if (time2.hours > time1.hours)
eTime.hours = time2.hours - time1.hours;
else
eTime.hours = (24 - time1.hours) + time2.hours;
if (time2.minutes > time1.minutes)
eTime.minutes = time2.minutes - time1.minutes;
else
eTime.minutes = (60 - time1.minutes) + time2.minutes;
if (time2.seconds > time1.seconds)
eTime.seconds = time2.seconds - time1.seconds;
else
eTime.seconds = (60 - time1.seconds) + time2.seconds;
return eTime;
}
//Function to check if inputed times are within valid parameters
void checkTime (struct time a)
{
if(a.hours < 0 || a.hours > 23) {
printf("Please enter a valid figure for hours in the range 1 to 23\n");
error = 1;
}
if(a.minutes < 0 || a.minutes > 59) {
printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
error = 1;
}
if(a.seconds < 0 || a.seconds > 59) {
printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
error = 1;
}
else {
error = 0;
}
}
int main (void)
{
struct time time1;
struct time time2;
struct time main_eTime;
printf("Enter time 1 in the format hours:mins:sec\n");
scanf("%i:%i:%i", &time1.hours, &time1.minutes, &time1.seconds);
checkTime(time1);
while(error == 1) {
printf("Enter time 1 in the format hours:mins:sec\n");
scanf("%i:%i:%i", &time1.hours, &time1.minutes, &time1.seconds);
}
printf("Enter time 2 in the format hours:mins:sec\n");
scanf("%i:%i:%i", &time2.hours, &time2.minutes, &time2.seconds);
checkTime(time2);
while(error == 1) {
printf("Enter time 2 in the format hours:mins:sec\n");
scanf("%i:%i:%i", &time2.hours, &time2.minutes, &time2.seconds);
}
main_eTime = elapsed_time(time1, time2);
printf("Elapsed time between time 1 and time 2 is %i:%i:%i\n", main_eTime.hours,
main_eTime.minutes, main_eTime.seconds);
return 0;
}
我认为您的分支条件有问题,尤其是在 checkTime
函数中。如果秒有效,最后的else
条件将执行,它与您输入的小时和分钟是否正确无关。
改为:
void checkTime (struct time a)
{
if(a.hours < 0 || a.hours > 23) {
printf("Please enter a valid figure for hours in the range 1 to 23\n");
error = 1;
}
else if(a.minutes < 0 || a.minutes > 59) {
printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
error = 1;
}
else if(a.seconds < 0 || a.seconds > 59) {
printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
error = 1;
}
else {
error = 0;
}
}
只有当所有三个变量(小时、分钟和秒都有效)时,以上才会 运行 else
阻塞。
您的代码结构是:
if (COND_1) {
...
}
if (COND_2) {
...
}
if (COND_3) {
...
}
else {
...
}
如果 COND_1
、COND_2
或 COND_3
的 none 为真,您似乎认为 else
应该 运行。但这不是它的工作原理:else
附加到前面的 if
语句,仅此而已。 else
块是否 运行 唯一重要的部分是 COND_3
条件。
你应该做的是:
if (COND_1) {
...
}
else if (COND_2) {
...
}
else if (COND_3) {
...
}
else {
...
}
通过以这种方式构建代码,我们确保只执行一个块。
... 或:
error = 0;
if(a.hours < 0 || a.hours > 23) {
printf("Please enter a valid figure for hours in the range 1 to 23\n");
error = 1;
}
if(a.minutes < 0 || a.minutes > 59) {
printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
error = 1;
}
if(a.seconds < 0 || a.seconds > 59) {
printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
error = 1;
}
通过在开始时设置 error = 0
,您根本不需要 else
块。