英文:
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("Enter an integer :: ");
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论