检查输入的数字是否为奇数

发布于 2024-10-21 16:00:28 字数 1269 浏览 5 评论 0原文

“我不确定要插入什么代码,甚至不知道在哪里插入,但我想检查我输入的数字是否为奇数。

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 技术交流群。

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

发布评论

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

评论(5

那小子欠揍 2024-10-28 16:00:28

使用模算术

if (number % 2 == 0) {
  // even
} else {
  // odd
}

更新:

您可以在此处测试此代码:

请注意,使用 number % 2 == 1 将失败。

要检查数字是否为奇数,可以使用 (number & 1) != 0

Use modular arithmetic:

if (number % 2 == 0) {
  // even
} else {
  // odd
}

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.

美人迟暮 2024-10-28 16:00:28

num % 2 == 1 对负奇数返回错误结果,与 2 除的余数不会为 1。
可以按如下方式修复此问题:

public boolean isOddNumber(int num) {
    return (num & 1) != 0;
}

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:

public boolean isOddNumber(int num) {
    return (num & 1) != 0;
}
风启觞 2024-10-28 16:00:28

您不能在该 if 中包含 readLine首先您需要获取值,然后您可以使用您的if

它是这样的:

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) {
// odd
} else {
  System.out.println("Please enter an odd number!");
}

最后 - 不要使用名为“a”、“e”或“d”的值 - 这非常令人困惑。只需使用让读者知道/猜测它们在代码中的角色的名称来命名变量即可。我不知道你的“a”或b、c、d等的含义是什么。例如,你的num应该命名为enteredValue以澄清你的代码。

You can NOT have readLine inside of that if. First you need to get the value and next you can use your if.

It goes like this:

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) {
// odd
} else {
  System.out.println("Please enter an odd number!");
}

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 named enteredValue to clarify your code.

肥爪爪 2024-10-28 16:00:28

Chaker关于负整数的回答得到了证实。
在我的 JavaSE-1.8 中

System.out.println( "result =" + ( -3 % 2 == 1) );

它显示结果 = false 而不是 true

Chaker's answer about negative integer is confirmed.
In my JavaSE-1.8

System.out.println( "result =" + ( -3 % 2 == 1) );

it shows result =false instead true

毅然前行 2024-10-28 16:00:28

Java 中的按位运算(位操作)方式的

if ((num & 1) != 0) //odd
{
     //do something here
} else { //even
    //do something here
}

工作方式是查看 100(数字 4)与 001,对每个位执行 AND 运算并返回 0 和 1。如果结束位是 0,例如 4,则它始终是将会变得均匀。如果结束位为 1,则为奇数。

Bitwise operation (bit manipulation) way in Java

if ((num & 1) != 0) //odd
{
     //do something here
} else { //even
    //do something here
}

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.

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