( 0 < 0 ) 返回 true 吗?

发布于 2024-08-29 16:57:54 字数 118 浏览 3 评论 0 原文

我有一个 if 语句,

if (int1 < int2)
{}
else
{}

如果 int1 和 int2 都为 0,我希望运行 else 语句。

ill have an if statement

if (int1 < int2)
{}
else
{}

I want the else statement to run if both int1 and int2 are 0..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

花之痕靓丽 2024-09-05 16:57:54

否。0 不小于0

使用 else if 语句怎么样?

if (int1 < int2)
{
}
else if (int1 == 0 && int2 == 0)
{
}

No. 0 is not less than 0.

How about using an else if statement?

if (int1 < int2)
{
}
else if (int1 == 0 && int2 == 0)
{
}
烟凡古楼 2024-09-05 16:57:54

对于您当前的问题有几个答案,本页上还有很多其他答案。但还有一个更大的问题尚未得到解决:为什么你要向网络论坛提出这个问题,无论有多少专家程序员?

有一个优秀的工具可供您使用,可以自动回答此类问题,并且您一直在使用它:您的 C 编译器!您只需正确表述问题即可。

如果有什么不明白的地方,请尝试编写一个小程序来测试一些逻辑,看看会发生什么。只需保留一个简单的模板(我喜欢 ~/tmp/hello_world.c)。当您有问题时,只需复制一份(例如 ~/tmp/zerotest.c),添加一些您想尝试的功能(例如 printf("Answer: %d\n", 0 < 0 );),然后运行它,直到您了解发生了什么。

我一直这样做。即使当我正在处理另一个项目时,有时我也会将逻辑块提取到一个小文件中并在那里使用它,直到我理解。这就是本文的目的:找到一种有效的方法来自学语言。不要害怕尝试。您极不可能将某些东西拼凑在一起而破坏您的系统。即使发生这种情况,我相信您也可以从这次经历中学到一些东西。

养成尝试的习惯。这是一项您将在余下的编程生涯中用到的技能。

There are several answer to your immediate question, and plenty of other answers on this page. But there's a larger question that hasn't been addressed yet: why are you asking this question to a web forum, no matter how many expert programmers populate it?

There is an excellent tool at your disposal for automatically answering such questions, and you use it all the time: your C compiler! You just have to phrase the question correctly.

If there's something you don't understand, try putting together a small program to test out some logic and see what happens. Just keep around a simple template (I like ~/tmp/hello_world.c). When you have a question, just make a copy (say, ~/tmp/zerotest.c), add some feature you want to try out (like printf("Answer: %d\n", 0 < 0);), and run it until you understand what's going on.

I do this all the time. Even when I'm working on another project, sometimes I pull chunks of logic out into a little file and play around with it there until I understand. That's what this is about: finding an efficient way to teach yourself a language. Don't be afraid to experiment. It's extremely unlikely you will slap something together that will destroy your system. And even if that happens, I'm sure you can learn something from that experience too.

Get in the habit of experimenting. It's a skill you'll use for the rest of your programming career.

叹沉浮 2024-09-05 16:57:54

(0 < 0) 逻辑上应该返回 false,因为两个相等的数字之间,一个不能小于另一个。 (0 <= 0) 将返回 true。

(0 < 0) should logically return false, as between two equal numbers, one can not be less than the other. (0 <= 0) would return true.

迷离° 2024-09-05 16:57:54

目前,当两个 int 都为 0 时,您的 else 子句将运行。

如果您希望将 0 视为与 int1 << int2 那么

if( (int1 < int2) || (int1 == 0 && int2 ==0) )

但如果你只想 0 < 0 去其他,它会......

或者也许你有一些代码,你认为应该去你的“其他”,但正在输入你的“如果”,但无法弄清楚为什么所以想知道是否 0 < 0?在这种情况下,您的代码中可能会发生其他情况。

Currently your else clause will run when both ints are 0.

if you want 0s to be treated the same as int1 < int2 then

if( (int1 < int2) || (int1 == 0 && int2 ==0) )

but if you just want 0 < 0 to go to the else, it will...

or perhaps you have some code that you think should be going to your "else", but is entering your "if", but can't work out why so are wondering if 0 < 0? In which case, something else is likely going on in your code.

梦纸 2024-09-05 16:57:54

否,(0<0) 返回 false。

很难理解您在所提供的代码的上下文中提出的逻辑。

No, (0<0) returns false.

It's difficult to understand the logic you propose in the context of the presented code.

初相遇 2024-09-05 16:57:54

当然,0==0NOT 0<00>0

在编译语言中,例如c# 您需要对每个变量使用比较运算符,如上面答案中所述

if (int1 < int2)
{
}
否则如果(int1 == 0 && int2 == 0)
{
}

但是,如果您的情况是某种解释语言,例如 python,您可以使用简单的内联比较

if int1<int2:
     print "Less"
elif int1==int2==0:
     print "Equals to 0"

Of-course, 0==0 and NOT 0<0 or 0>0

In compiled languages such a c# you need to use comparison operator for each variable as stated in the above answers

if (int1 < int2)
{
}
else if (int1 == 0 && int2 == 0)
{
}

But, if your case is of some interpreted language such as python, you can use a simple inline comparison

if int1<int2:
     print "Less"
elif int1==int2==0:
     print "Equals to 0"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文