2013-12-11 174 views
-3

我試圖輸出一個真或假的值,並有編譯困難。你不能打印布爾方法的值嗎?無法編譯類

錯誤:

javac ArrayManip.java 
ArrayManip.java:23: error: <identifier> expected 
     System.out.println(valuesSymmetrical);     ^
ArrayManip.java:23: error: <identifier> expected 
     System.out.println(valuesSymmetrical); 

public class ArrayManip 
{ 
public static void main(String [] args) 
    { 
     int numbers [] = {2,3,4,5,6,6,5,4,3,2}; 
     boolean areThey = valuesSymmetrical(numbers); 

    } 

    public static boolean valuesSymmetrical(int [] n) 
    { 
    int i = 0, y = n.length - 1; 
    while (n[i] == n[j] && i < j) 
    { 
     i++; 
     j--; 
    } 

    if (i < j) return true; 
    else return false; 
} 

    System.out.println(valuesSymmetrical); 
} 
+2

1.你意識到'的System.out.println(valuesSymmetrical);'是之外的任何方法的範圍? 2.如果你想調用方法,你需要像在你的情況'valuesSymmetrical(arrayOfIntegers)'中那樣添加它的參數。 3.什麼是'valuesSymmetrical'方法中的'j'? – Pshemo

+0

嘗試從正確格式化代碼開始,使用適當的縮進可以很容易地找到問題。 – turbo

+0

與你的問題沒有太大關係,但我相當肯定你想把'if(i j)' –

回答

3
System.out.println(valuesSymmetrical); 

是任何方法之外。

嘗試這樣的:

public class ArrayManip 
{ 
    public static void main(String [] args) 
    { 
    int numbers [] = {2,3,4,5,6,6,5,4,3,2}; 
    boolean areThey = valuesSymmetrical(numbers); 

    System.out.println(areThey); 

    } 

    public static boolean valuesSymmetrical(int [] n) 
    { 
    int i = 0, j = n.length - 1; // notice here you had y and not j! 
    while (n[i] == n[j] && i < j) 
    { 
     i++; 
     j--; 
    } 

    if (i < j) return true; 
    else return false; 
    } 
} 
2

你需要真正通話的功能,例如,

System.out.println(valuesSymmetrical(numbers)); 

main方法將這個。


無關:爲了你和別人的,請始終格式化你的代碼,例如,

public class ArrayManip { 

    public static boolean valuesSymmetrical(int [] n) { 
     int i = 0, y = n.length - 1; 
     while (n[i] == n[j] && i < j) { 
      i++; 
      j--; 
     } 

     return i < j; 
    } 

    public static void main(String args[]) { 
     int numbers[] = { 2, 3, 4, 5, 6, 6, 5, 4, 3, 2 }; 
     System.out.println(valuesSymmetrical(numbers)); 
    } 

} 

(這不能解決任何邏輯錯誤。)

0

移動你的系統輸出了進入你的主要方法。

System.out.println(areThey); 

System.out.println(valuesSymmetrical(numbers));