2014-10-18 12 views
-5
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. 
+0

'a/2'不是一個布爾類型。您可能想要添加一些比較運算符。它也不會正確檢查奇偶校驗。 'System.print.out'應該是'System.out.print'。 – Zong 2014-10-18 16:59:28

+0

謝謝,但我仍然不能解決一個/ 2 是否有任何其他方式來解決這個錯誤 – 2014-10-18 17:04:52

+0

你需要像'if(a/2 == 0)''不只是'如果(a/2)'''。另外,你可能想看看'%'(模)運算符。 – csmckelvey 2014-10-18 17:05:52

回答

1

下面是代碼:

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"); 
      } 
     } 
    } 

運行它,你會得到輸出。

注意:運行代碼後,請閱讀上面的註釋,作爲他們必須說的並嘗試遵循。這隻會改善你