如何编写一个在Java中判断数字是奇数还是偶数并打印输出的语句

huangapple go评论95阅读模式
英文:

How Can I Write A Statement To Print Out If The Number Is Odd Or Even in Java

问题

我想编写一个程序来检查并打印出一个数字是奇数还是偶数。
到目前为止,这是我已经编写的程序:

import java.util.Scanner;
public class Irs_Lab
{
    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);
        System.out.print("输入一个整数 :: ");
        int num = kb.nextInt();
    }
}

我听说你可以使用 %(取模)来获取除法的余数。
有人可以帮助我吗?

英文:

I want to write A program To check and Print Out If The Number Is Odd Or Even.<br>
This Is What I Have Programmed So Far:

import java.util.Scanner;
public class Irs_Lab
{
    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);
        System.out.print(&quot;Enter an integer :: &quot;);
        int num = kb.nextInt();
    }
}

I Heard You can Use A % (mod) to get the remainder of division.<br>
Can anyone help me on this.

答案1

得分: 1

你可以使用模运算符来判断一个数字是偶数还是奇数(就像你说的那样)。当一个数字被2除时,如果它是偶数,余数就是0;如果是奇数,余数就是1。使用 if(a % 2 == 0){//偶数}else{//奇数} 是你可能的解决方案。

英文:

You can use the modular operator to see if a number is even or odd(as you said). When dividing a number by 2, if it is even it will have a remainder of 0, if it is odd it will have a remainder of 1. Using if(a % 2 == 0){//even}else{//odd} is your likely solution

huangapple
  • 本文由 发表于 2020年8月18日 08:26:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63460243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定