Java 不使用条件码判断数字是奇数还是偶数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20705184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine whether number is odd or even without using conditional code
提问by user3122903
How to find whether a number is odd or even, without using if
condition or ternary operators in Java?
如何if
在Java中不使用条件或三元运算符的情况下查找数字是奇数还是偶数?
This question is given by my teacher. He also give me a hint that it is possible by using a bitwise operator.
这个问题是我老师给的。他还暗示我可以使用按位运算符。
采纳答案by Pshemo
There are few ways to not use if
and get behavior that will be same as if if
was used, like ternary operator condition ? valueIfTrue : valueIfFalse
or switch/case
.
有几种方法可以不使用if
和获取与if
使用时相同的行为,例如三元运算符condition ? valueIfTrue : valueIfFalse
或switch/case
.
But to be tricky you can also use arrays and try to figure some transformationof our value to proper array index. In this case your code could look like
但棘手的是,您也可以使用数组并尝试将我们的值转换为适当的数组索引。在这种情况下,您的代码可能看起来像
int number = 13;
String[] trick = { "even", "odd" };
System.out.println(number + " is " + trick[number % 2]);
output:
输出:
13 is odd
You can change number % 2
with number & 1
to use suggestion of your teacher. Explanation of how it works can be found here.
您可以更改number % 2
与number & 1
使用你的老师的建议。可以在此处找到其工作原理的说明。
回答by umanganiello
int isOdd = (number & 1);
isOdd
will be 1
if number is odd, otherwise it will be 0
.
isOdd
会1
如果数为奇数,否则会0
。
回答by Mureinik
Consider a number's representation in binary format (E.g., 5 would be 0b101). An odd number has a "1" as its singles digit, an even number had a zero there. So all you have to do is bitwise-and it with 1 to extract only that digit, and examine the result:
考虑以二进制格式表示的数字(例如,5 将是 0b101)。奇数的单数是“1”,偶数是零。所以你所要做的就是按位 - 并用 1 只提取那个数字,并检查结果:
public static boolean isEven (int num) {
return (num & 1) == 0;
}
回答by gabriel
Just saw now 'Without using IF'
现在才看到 'Without using IF'
boolean isEven(double num) { return (num % 2 == 0) }
回答by Arne Burmeister
Did you mean something like this?
你的意思是这样的吗?
boolean isEven(int value) {
return value % 2 == 0;
}
boolean isOdd(int value) {
return value % 2 == 1;
}
回答by SANN3
Every odd number have 1 at the end of its binary representation.
每个奇数在其二进制表示的末尾都有 1。
Sample :
样本 :
public static boolean isEven(int num) {
return (num & 1) == 0;
}
回答by boxed__l
Just a quick wrapper over the already defined process...
只是对已经定义的过程的快速包装......
public String OddEven(int n){
String oe[] = new String[]{"even","odd"};
return oe[n & 1];
}
回答by iPot
I would use:
我会用:
( (x%2)==0 ? return "x is even" : return "x is odd");
One line code.
一行代码。
回答by Satvant Singh
# /* **this program find number is odd or even without using if-else,
## switch-case, neither any java library function...*/
##//find odd and even number without using any condition
class OddEven {
public static void main(String[] args) {
int number = 14;
String[] trick = { "even", "odd" };
System.out.println(number + " is " + trick[number % 2]);
}
}
/**************OUTPUT*************
// 14 is even
// ...................
//13 is odd
回答by prashant
Method 1:
方法一:
System.out.println(new String[]{"even","odd"}[Math.abs(n%2)]);
Method 2:
方法二:
System.out.println(new String[]{"odd","even"}[(n|1)-n]);
Method 1 differs from the accepted answer in the way that it accounts for negative numbers as well, which are also considered for even/odd.
方法 1 与接受的答案的不同之处在于它也考虑了负数,也考虑了偶数/奇数。