检查输入的数字是否为奇数
“我不确定要插入什么代码,甚至不知道在哪里插入,但我想检查我输入的数字是否为奇数。
import java.io.*;
import javax.swing.JOptionPane;
public class Diamond {
public static void main(String [] args) throws IOException {
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
String input;
int num;
System.out.println("input number: ");
input = stdin.readLine ();
num = Integer.parseInt(input);
if (num % 2 ==1){
int d = num;
int e = 0;
for (int a = 0; a <= num; a++) {
for (int c = d; c>= 1; c-- )
System.out.print(" ");
d-=1;
for (int b = 1; b <= a; b++)
System.out.print ("* ");
System.out.println();
}
num-=1;
for (int a = 0; a<=num; a++) {
for (int b = num; b > a; b--)
System.out.print (" *");
System.out.println();
for (int c = 0; c <= e; c++)
System.out.print(" ");
e+=1;
}
} else {
System.out.println("Please enter an odd number!");
}
}
}
`I'm not sure what code to insert or even where, but I would like to check the number I enter is an odd number.
import java.io.*;
import javax.swing.JOptionPane;
public class Diamond {
public static void main(String [] args) throws IOException {
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
String input;
int num;
System.out.println("input number: ");
input = stdin.readLine ();
num = Integer.parseInt(input);
if (num % 2 ==1){
int d = num;
int e = 0;
for (int a = 0; a <= num; a++) {
for (int c = d; c>= 1; c-- )
System.out.print(" ");
d-=1;
for (int b = 1; b <= a; b++)
System.out.print ("* ");
System.out.println();
}
num-=1;
for (int a = 0; a<=num; a++) {
for (int b = num; b > a; b--)
System.out.print (" *");
System.out.println();
for (int c = 0; c <= e; c++)
System.out.print(" ");
e+=1;
}
} else {
System.out.println("Please enter an odd number!");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用模算术:
更新:
您可以在此处测试此代码:
请注意,使用
number % 2 == 1
将失败。要检查数字是否为奇数,可以使用
(number & 1) != 0
。Use modular arithmetic:
Update:
You can test this code here:
Beware that checking for evenness with
number % 2 == 1
will fail.To check if a number is odd, you can use
(number & 1) != 0
.num % 2 == 1 对负奇数返回错误结果,与 2 除的余数不会为 1。
可以按如下方式修复此问题:
num % 2 == 1 return incorrect results on negative Odd number, the remainder of division with 2 will not be 1.
This can be fixed as follows:
您不能在该
if
中包含readLine
。 首先您需要获取值,然后您可以使用您的if
。它是这样的:
最后 - 不要使用名为“a”、“e”或“d”的值 - 这非常令人困惑。只需使用让读者知道/猜测它们在代码中的角色的名称来命名变量即可。我不知道你的“a”或b、c、d等的含义是什么。例如,你的
num
应该命名为enteredValue
以澄清你的代码。You can NOT have
readLine
inside of thatif
. First you need to get the value and next you can use yourif
.It goes like this:
Finally - do NOT use values named "a", "e" or "d" - it's very confusing. Just name vars with names that let reader know/guess their role in your code. I have no idea what is the meaning of your "a" or b, c, d etc. For example, your
num
should be namedenteredValue
to clarify your code.Chaker关于负整数的回答得到了证实。
在我的 JavaSE-1.8 中
它显示结果 = false 而不是 true
Chaker's answer about negative integer is confirmed.
In my JavaSE-1.8
it shows result =false instead true
Java 中的按位运算(位操作)方式的
工作方式是查看 100(数字 4)与 001,对每个位执行 AND 运算并返回 0 和 1。如果结束位是 0,例如 4,则它始终是将会变得均匀。如果结束位为 1,则为奇数。
Bitwise operation (bit manipulation) way in Java
works by looking at say, 100 (number 4) vs 001, doing the AND operation on each bits and returning either 0 and 1. If the end bit is a 0, like 4, it's always going to be even. If the end bit is 1, it is going to be odd.