简体   繁体   English

执行 while 循环以请求正输入且小于 10

[英]do while loop to ask for positive input and less than 10

I'm new to C and I was asked to make a loop that asks a user to input a number and then check if that number is positive and less than 10. However, I don't know what's wrong with the code below.我是 C 的新手,我被要求创建一个循环,要求用户输入一个数字,然后检查该数字是否为正数且小于 10。但是,我不知道下面的代码有什么问题。 Any help is much appreciated.任何帮助深表感谢。

int main(){
int a;

do{
    printf("Enter a positive number: ");
    scanf("%i", &a);
}while((a <= 0) && (a >= 10));

    printf("%i is positive and less than 10", a);

return 0;
}

There are two problems that need fixing:有两个问题需要解决:

  • You're not checking that scanf() succeeds.您没有检查scanf()是否成功。 I/O is brittle and can fail. I/O 很脆弱,可能会失败。
  • The condition is wrong, both branches of the && cannot be true at the same time, thus "and" is the wrong operator.条件错误, &&的两个分支不能同时为真,因此“and”是错误的运算符。 You meant ||你的意思是|| (boolean "or"). (布尔“或”)。

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

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