Java中的未知错误?

时间:2014-01-30 23:06:08

标签: java arrays arraylist

所以我昨晚发布了一些问题,但我的代码很乱,所以我重写了它。首先,它需要做的是打印一个3x3的数字网格,然后找到所有的马鞍并打印出来。 (鞍座是列中最低但行中最高的数字)。

运行时,这是控制台输出:

7 1 18
15 10 3
0 15 2

马鞍是:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The operator < is undefined for the argument type(s) int[], int[]
    The operator < is undefined for the argument type(s) int[], int[]
    Type mismatch: cannot convert from int[] to int
    The operator < is undefined for the argument type(s) int[], int[]
    The operator < is undefined for the argument type(s) int[], int[]
    Type mismatch: cannot convert from int[] to int
    The operator < is undefined for the argument type(s) int[], int[]
    The operator < is undefined for the argument type(s) int[], int[]
    Type mismatch: cannot convert from int[] to int
    at Main.findLowest(Main.java:66)
    at Main.findSaddles(Main.java:54)
    at Main.main(Main.java:16)

这是我的代码,有点乱,但还没有时间清理它:

    import java.util.ArrayList;
    import java.util.Random;

    public class Main {

    static  int[][] Grid = new int[3][3];
    static ArrayList<Integer> Saddles = new ArrayList<Integer>();
    static Random random = new Random();
    static int currentColumn; //will be use by various methods to fill/read from array


    public static void main(String[] args) {
        fillArray();
        printArray();
        findSaddles();
    }//end of main()


    //fill the array with random "ints"
    public static void fillArray(){
        currentColumn = 0;
        int indexsFilled = 0;

        while(indexsFilled != 9){
            Grid[0][currentColumn] = random.nextInt(21);
            Grid[1][currentColumn] = random.nextInt(21);
            Grid[2][currentColumn] = random.nextInt(21);
            indexsFilled += 3;
            currentColumn += 1;         
        }//end of while loop    
    }//end fillArray()


    //print array's contents
    public static void printArray(){
        currentColumn = 0;
        int indexsPrinted = 0;

        while(indexsPrinted != 9){
            System.out.print(Grid[0][currentColumn] + " ");
            System.out.print(Grid[1][currentColumn] + " ");
            System.out.print(Grid[2][currentColumn] + "\n");
            indexsPrinted += 3;
            currentColumn += 1;         
        }//end of while loop
    }//end of printArray()


    public static void findSaddles(){
        System.out.println("The saddles are: ");

        currentColumn = 0;
        findLowest(currentColumn);
        findLowest(currentColumn + 1);
        findLowest(currentColumn + 2);

        for(int i = 0; i <= Saddles.size(); i++){
            System.out.print(Saddles.get(i) + " ");
        }

    }//end find Saddles

    public static void findLowest(int columnNumber){
        int lowest;
        if(Grid[0] < Grid[1] || Grid[0] < Grid[2]){
            lowest = Grid[0];
        }
        else{
            if(Grid[1] < Grid[0] || Grid[1] < Grid[2]){
                lowest = Grid[1];
            }
            else{
                if(Grid[2] < Grid[1] || Grid[2] < Grid[1])
                    lowest = Grid[2];
            }//end branching if loop
        }
        Saddles.add(lowest);
    }//end of findLowest

}//end of Main

4 个答案:

答案 0 :(得分:1)

您有一个2D数组Grid,但在findLowest中,您只使用一个数组索引来访问要与<进行比较的值。这将检索2D数组中的1D数组。

使用两个数组索引来访问2D数组中的值。

if(Grid[0][columnNumber] < Grid[1][columnNumber] ||
   Grid[0][columnNumber] < Grid[2][columnNumber]){

同样适用于该方法中的其他Grid 2D数组访问。

答案 1 :(得分:0)

您正在尝试比较数组,并且未在java中定义:

//...
static  int[][] Grid = new int[3][3];
//...
if(Grid[0] < Grid[1] || Grid[0] < Grid[2]) { // This comparissons are impossible
    lowest = Grid[0];
}

您无法直接比较数组。您需要比较每个条目。

答案 2 :(得分:0)

您的所有错误似乎都来自Grid[i] {对于任何i} int。例如,你有:

if(Grid[0] < Grid[1] || Grid[0] < Grid[2]){
    lowest = Grid[0];
}

但是Grid是一个数组数组,因此Grid[0](例如)是一个数组,而不是int值。

答案 3 :(得分:0)

public static void findLowest(int columnNumber){
    int lowest;
    if(Grid[0] < Grid[1] || Grid[0] < Grid[2]){
        lowest = Grid[0];
    }
    else{
        if(Grid[1] < Grid[0] || Grid[1] < Grid[2]){
            lowest = Grid[1];
        }
        else{
            if(Grid[2] < Grid[1] || Grid[2] < Grid[1])
                 lowest = Grid[2];
        }//end branching if loop
    }
    Saddles.add(lowest);
}//end of findLowest

您根本没有使用'columnNumber'参数。您需要使用它来引用您引用的'Grid'中的哪个数组,然后对该数组的元素执行比较。一个更正的例子:

public static void findLowest(int columnNumber)
{
    // ...
    if (Grid[columnNumber][0] < Grid[columnNumber][1] || Grid[columnNumber][0] < Grid[columnNumber][2])
    // Rest of your code in a similar style
}