2015-09-27 87 views
-9

我有這樣的程序設置和我需要2個錯誤是幫助我得到錯誤「其他」沒有「如果」

import java.util.Scanner; 

public class EvenOdd { 
    public static void main(String[]args) { 
     Scanner keyboard = new Scanner (System.in); 
     System.out.print("---EvenOdd--- /n"); 
     System.out.printf("Enter a whole number: "); 
     c = in.nextInt(); 
    } 

    public static EvenOdd (int num) { 
     int c = num; 

     if (int.class) 
      (c/2)*2 = c; 

     System.out.println("is even."); 

     else (c) 
      System.out.println("is odd"); 

     return EvenOdd; 
    } 
} 
C:\Users\Desktop\EvenOdd.java:28: error: not a statement 
else (c) 

C:\Users\Desktop\EvenOdd.java:28: error: 'else' without 'if' 
else (c) 

2 errors 
+1

使用大括號。 'if'後面有兩條語句。 – user2864740

+0

我試過了,但它不斷給我提供越來越多的錯誤 –

+0

這根本不是合適的Java語法。 'if'後面的表達式應該是*'boolean' *,並且不應該在else之後有一個表達式(在括號中),只有一個語句。請參閱[官方教程](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html)。 – RealSkeptic

回答

2

您還有沒有意義。首先,你沒有使用大括號,但你的布爾邏輯沒有意義。只需添加大括號不會讓您的代碼編譯。

我覺得這改寫是最接近你有什麼

public static boolean EvenOdd (int num) { 
    // Here your calculation is done 
    boolean isEven = (c/2)*2 == c; 

    if (isEven) { 
     System.out.println("is even."); 
    } else if (!isEven) { 
     // Using 'else if' for a boolean parameter does not make much sense 
     // but i'll leave it here to explain the syntax 
     System.out.println("is odd"); 
    } 
    return isEven; 
} 

然而,最常見的方式來檢查奇數或偶數使用模運算符。如果我使整個碼多一點的java十歲上下,你最終用(例如方法命名)

/** 
* Check if the given number is even. 
* @param num the number to check 
* @return whether num is an even number 
*/ 
public static boolean isEven (int num) { 
    if ((x % 2) == 0) { 
     System.out.println("is even."); 
     return true; 
    } else { 
     System.out.println("is odd"); 
     return false; 
    } 
} 
2

這裏是一個解決方案

public static void main(String[]args) 
{ 
    Scanner keyboard = new Scanner(System.in); 

    try{ 
     System.out.println("---EvenOdd---"); 

     System.out.print("Enter a whole number: "); 
     int c = keyboard.nextInt(); 

     evenOdd(c); 
    }finally{ 
     keyboard.close(); 
    } 
} 

public static void evenOdd(int num) 
{ 
    int c = num; 
    if ((c/2)*2 == c){ 
     System.out.println("is even."); 
    }else { 
     System.out.println("is odd"); 
    } 
} 

輸出:

爲輸入值5

--- --- EVENODD

輸入一個整數:5

是奇數

爲輸入值4

--- --- EVENODD

輸入一個整數:4

是偶數。

續讀

有幾個問題與原來的代碼,我將試圖解釋他們在網上訂購。參考

原始代碼:

public class EvenOdd { 
    public static void main(String[]args) { 
     Scanner keyboard = new Scanner (System.in); 
     System.out.print("---EvenOdd--- /n"); 
     System.out.printf("Enter a whole number: "); 
     c = in.nextInt(); 
    } 

    public static EvenOdd (int num) { 
     int c = num; 

     if (int.class) 
      (c/2)*2 = c; 

     System.out.println("is even."); 

     else (c) 
      System.out.println("is odd"); 

     return EvenOdd; 
    } 
} 

首先我們這行

System.out.print("---EvenOdd--- /n");

使用這裏的.print()方法,雖然不違法是不必要的,因爲Java的自動爲我們提供了.println()這創建一個新的行,所以我們不必。 (即具有「/ N」)

System.out.printf("Enter a whole number: ");

接着使用.printf()方法,該打印格式的輸出和接受參數作爲參數。您沒有使用此方法的任何專有功能,因此我們可以使用.print()實現相同的功能。

c = in.nextInt();

變量in並不在此範圍內定義的,我假定你的意思是使用keyboard.nextInt()

public static EvenOdd (int num) {

當方法沒有返回類型和相同的名稱,因爲它駐留在類(區分大小寫)它是一個構造函數。構造函數通常不需要return語句,而是通過語法new ObjectConstructor()調用,以便爲與構造函數相同類型的變量賦值。

if (int.class) 
(c/2)*2 = c; 
System.out.println("is even."); 
else (c) 
System.out.println("is odd"); 

if-else塊顯然是連java語法。

首先沒有必要將結果轉換爲int,並且條件結尾的分號不屬於。

消除這些錯誤給我們帶來:

if (c/2)*2 = c 
System.out.println("is even."); 
else (c) 
System.out.println("is odd"); 

現在我們需要來包裝我們的條件括號內爲()'而非使用賦值運算符「=」我們應該用比較操作符'== '返回boolean。此外,else子句不需要條件,如果您想使用條件查看elseif

這些變化讓我們走到這一步。

if ((c/2)*2 == c) 
System.out.println("is even."); 
else 
System.out.println("is odd"); 

現在我們添加適當的括號,我們很好去。

if ((c/2)*2 == c){ 
    System.out.println("is even."); 
}else{ 
    System.out.println("is odd"); 
}