四个值是非零和非负的
假设我有四个变量x,y,z,w。我想打印:
“ Hello All零值”,仅当所有X,Y,Z,W都是非阴性和非零值时。
如果所有值为零,则打印“ Hello Zero”。
如果任何值(一个,两个或三个但不是全部)为零或负数,则打印“非法值”。
我编写了一个样本解决方案,该解决方案处理和俱乐部负数以及非负值:
if((x&y&z&w) == 0 && !(x==y && y==z && z==w && w==0)) {
System.out.println("illegal values");
} else {
System.out.println("hello all non zero values");
}
但是,我无法分别处理负值和正值。谁能为此提出解决方案?
Suppose I have four variables x, y, z, w. I want to print:
"hello all non zero values" only if all x, y, z, w are non-negative and non-zero values.
If all the values are zero, then print "hello zero".
If any of the values (one, two or three but not all of them) is zero or negative then print "illegal values".
I've written a sample solution which handles and clubs negative as well as non-negative values:
if((x&y&z&w) == 0 && !(x==y && y==z && z==w && w==0)) {
System.out.println("illegal values");
} else {
System.out.println("hello all non zero values");
}
However, I am not able to handle the negative and positive values separately. Can anyone please suggest a solution for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
位填充方法:
这起作用是因为钻头 - 如果四个值中的任何一个为零,则位于零,如果四个值中的任何一个均非零,则位 - 或非零;如果在四个值中的任何一个中设置了符号位,则在结果中设置符号位(即负)。
(而且我使用临时“ s”,因为为什么要对同一例程编写3个调用)
编辑:此答案在最近对问题进行了编辑后进行了更新,该问题已阐明了标准。
The bit-fiddling approach:
This works because the bitwise-and is zero if any of the four values is zero, the bitwise-or is non-zero if any of the four values is non-zero; and the sign bit is set in the result (i.e., negative) if the sign bit is set in any of the four values.
(And I use the temporary 's' because why write 3 calls to the same routine)
Edited: this answer was updated after a recent edit to the question, which has clarified the criteria.
这可能有助于以不同但同等的方式重新介绍您的问题。您基本上是在打印“ Hello Zero”,如果所有这些都为零,则“ Hello asher All Zero value”,如果所有这些值都是 atenty 和“非法值”,则在所有其他情况下 。
It might help to reword your question in a different but equivalent way. You are basically printing "hello zero" if all of them are zero, "hello all non zero values" if all of them are positive, and "illegal values" in all other cases.
这样的东西?
Something like this?
这个适合您吗?
编辑:这并不能完全回答这个问题,我很抱歉
Does this work for you?
Edit: this doesn't fully answer the question, my apologies