【问题标题】:Unknown error in Java?Java中的未知错误?
【发布时间】:2014-01-30 23:06:08
【问题描述】:

所以我昨晚发布了一些问题,但我的代码一团糟,所以我重新编写了它。首先它需要做的是打印一个 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

【问题讨论】:

  • 1) 错误告诉您确切地出了什么问题——您试图在数组上使用小于和大于,而不是在数组持有的项目上,和 2) 你正在尝试运行无法编译的代码——永远不要这样做。先修复编译问题。
  • 乍一看,您的findLowest() 函数实现似乎是错误的。您的算法尝试比较两个数组而不是两个数字。
  • "bit messy but haven't had time to clean it up yet:" -- 请在此处发布之前花点时间解决此问题。这意味着类似“我没有时间解决这个问题,但我希望你有时间阅读它”。我们非常感谢您确实确实花时间让我们更轻松地为您提供帮助。
  • 我不明白为什么人们会发布这类问题。你甚至读过错误吗?你要么不想读它,要么你读了它,但你仍然感到困惑。
  • 了解我们大多数人都是自学成才的,包括我自己。

标签: java arrays arraylist


【解决方案1】:

您有一个二维数组Grid,但在findLowest 中,您只使用一个数组索引来访问值以与&lt; 进行比较。检索二维数组中的一维数组。

使用两个数组索引来访问二维数组中的值。

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

对于该方法中的其他Grid 2D 数组访问也是如此。

【讨论】:

    【解决方案2】:

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

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

    您不能直接比较数组。您需要比较每个条目。

    【讨论】:

      【解决方案3】:

      您所有的错误似乎都来自尝试将Grid[i](对于任何i)作为int。例如,您有:

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

      Grid 是一个数组数组,所以Grid[0](例如)是一个数组,而不是int 值。

      【讨论】:

        【解决方案4】:
        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”参数。您需要使用它来引用您所指的“网格”中的哪个数组,然后对该数组的元素执行比较。一个更正的例子:

        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
        }
        

        【讨论】:

          猜你喜欢
          • 2023-03-26
          • 2014-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-17
          • 2013-04-13
          • 2018-12-17
          • 1970-01-01
          相关资源
          最近更新 更多