简体   繁体   English

强制输入大于或等于0且小于24

[英]Enforce input greater than or equal to 0 and less than 24

#include<stdio.h>

int main ()
{
int n,a=0,b=24;
do
  {
    scanf("%d",n); //ask the user to enter a value of n less than 24 
                  // but greater than 0. 
  } while(/*boolean expression logic*/)

if(a<n<b)
    {
    printf("%d\n",n);
    }
    return 0;
}

I need to evaluate: 我需要评估:

If the value of n is greater than or equal to 0 and less than 24 (less than or equal to 23) then 如果n的值大于或等于0且小于24(小于或等于23),则

.... go to the if statement and print the value of n ....转到if语句并打印n的值

otherwise 除此以外

... ask the user to input the value of n ie it should again go back in the loop. ...要求用户输入n的值,即应该再次返回循环。

You want the program to keep asking for values until n>=0 && n<24 ; 您希望程序继续询问值,直到n>=0 && n<24 ; in other words, you want to keep asking for values while !(n>=0 && n<24) , which using De Morgan's law we can write as !(n>=0) || !(n<24) 换句话说,您想在!(n>=0 && n<24)时继续询问值,根据De Morgan定律,我们可以写成!(n>=0) || !(n<24) !(n>=0) || !(n<24) , which can be reduced to n<0 || n>=24 !(n>=0) || !(n<24) ,可以减少为n<0 || n>=24 n<0 || n>=24

do
{
    scanf("%d",n);
}
while(n<0 || n>=24)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM