Java 免费节目
结果是:Java 免费节目,java,Java,结果是: public class evenVSodd { public static void main(String[] args) { int a; a = 4; if (a/2==0) { System.out.print("its an even number"); } else { System.out
public class evenVSodd
{
public static void main(String[] args)
{
int a;
a = 4;
if (a/2==0)
{
System.out.print("its an even number");
}
else
{
System.out.print("its a odd number");
}
}
}
代码如下:
----jGRASP exec: java evenVSodd
its a odd number
----jGRASP: operation complete.
运行它,您将获得输出
注意:在运行代码之后,阅读上面的注释作为他们必须说的内容,并尝试遵循。这只会改善您的情况a/2不是布尔类型。您可能需要添加一些比较运算符。它也不能正确地检查奇偶校验。System.print.out应该是System.out.print.thank,但我仍然无法解决a/2。是否有其他方法来解决该错误?您需要类似于a/2==0的东西,而不仅仅是a/2。另外,您可能想看看%模运算符。想想您的代码是什么意思。如果a=4怎么办?if2在Java中是什么意思?a=5怎么样?为什么基本上还是if2?你需要另一个运算符来确定奇偶校验。好的,我试过了,但结果是奇数!
import java.util.Scanner;
public class EvenAndOdd {
public static void main(String[] args) {
int a;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Number :");
a = sc.nextInt();
if(a%2==0) {
System.out.print("its an even number");
}
else {
System.out.print("its a odd number");
}
}
}