程序没有
问题描述:
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.
答
下面是代码:
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");
}
}
}
运行它,你会得到输出。
注意:运行代码后,请阅读上面的注释,作为他们必须说的并尝试遵循。这只会改善你
'a/2'不是一个布尔类型。您可能想要添加一些比较运算符。它也不会正确检查奇偶校验。 'System.print.out'应该是'System.out.print'。 – Zong 2014-10-18 16:59:28
谢谢,但我仍然不能解决一个/ 2 是否有任何其他方式来解决这个错误 – 2014-10-18 17:04:52
你需要像'if(a/2 == 0)''不只是'如果(a/2)'''。另外,你可能想看看'%'(模)运算符。 – csmckelvey 2014-10-18 17:05:52