无法编译类

时间:2013-12-11 19:45:25

标签: java

我正在尝试打印出真值或假值并且编译有困难。你能不打印布尔方法的值?

错误:

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

3 个答案:

答案 0 :(得分: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;
  }
} 

答案 1 :(得分: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));
    }

} 

(这不会纠正任何逻辑错误。)

答案 2 :(得分:0)

将您的sysout移动到主方法中。

System.out.println(areThey);

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