更新时间:2022-10-23 08:32:27
if (h1 == ' 2'&& h2大于' 3')
如果任何字符无效,此时此测试是多余的。它应该是初始if
语句的一部分。类似于:if ((h1< ' 0' || h1> ' 2')||(h2< ' 0' || h2> ' 9')||(m1< ' 0' || m1> ' 5' )||(m2< ' 0' || m2> ' 9')||(h1 == ' 2'&& h2> ' 3' ))
{
printf( 时间格式不是HH:MM。);
return 1 ;
}
I want to check if the input is in the time format HH:MM. Valid time is from 00:00 to 23:59. I did:
printf("Please enter you age: ");
scanf("%f", &age);
if (age < 18)
{
printf("Too young, we can't serve you.\n");
return 1;
}
printf("Please enter the time in the format HH:MM: ");
scanf("%c%c%c%c%c", &h1, &h2, &colon, &m1, &m2);
if ((h1 < '0' || h1 > '2') || (h2 < '0' || h2 > '9') || (m1 < '0' || m1 > '5') || (m2 < '0' || m2 > '9'))
{
if (h1 == '2' && h2 > '3')
{
printf("The time is not in the format HH:MM.");
return 1;
}
}
but it doesn't work.
if (h1 == '2' && h2 greater than '3')
If any character is invalid, this test is redundant at this point. It should be part of the initialif
statement. Something like:if ((h1 < '0' || h1 > '2') || (h2 < '0' || h2 > '9') || (m1 < '0' || m1 > '5') || (m2 < '0' || m2 > '9') || (h1 == '2' && h2 > '3')) { printf("The time is not in the format HH:MM."); return 1; }