如何确定一个值是否在范围内?

编程入门 行业动态 更新时间:2024-10-12 03:18:43
本文介绍了如何确定一个值是否在范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对C还是比较陌生,很不幸,我必须上学去上课,并且在最简单的练习中遇到问题。

I am relatively new to C, I have to do it for school unfortunately and I am having issues with it at the easiest exercises.

在这里,我必须检查一下如果一个数字在一定间隔内,例如4到6。我是这样的。

Here I have to check if a number is in a certain interval, for example between 4 and 6. I made it like this.

#include <stdio.h> int main(){ int i; printf("Value to check Interval \n"); scanf("%s", i); if (i>4 && i<6){ printf("%s Value is in first interval\n", i); } }

scanf 输入数字并检查它是否在间隔中。但是,即使我输入了一部分数字,例如5, printf 也不起作用。我还尝试为间隔之外的数字添加else语句,但是 printf 并没有改变任何内容。

The scanf to enter the number and check if it is in the interval. But even if I enter a number that is part of it, for example 5, the printf doesn't do anything. I tried also to add an else statement for numbers outside the interval, but also there the printf did not change anything.

推荐答案

这是因为您已将 i变量声明为int ,而将输入作为 string ,因此在检查条件时会在 i变量中获取 null 值,并且无法输入if阻止检查以下代码

It is because you have declared i variable as int and you are taking input as string so when it is checking condition it is getting null value in i variable and not able to enter if block check below code

#include <stdio.h> int main(){ int i; printf("Value to check Interval \n"); scanf("%d",&i); if (i>4 && i<6){ printf("%d Value is in first interval\n", i); } }

尝试编译您的代码,如果条件i变量不会返回空值

try compiling your code without if condition i variable will return a null value

更多推荐

如何确定一个值是否在范围内?

本文发布于:2023-11-07 19:26:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567215.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:范围内

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!