【发布时间】: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:"-- 请在此处发布之前花点时间解决此问题。这意味着类似“我没有时间解决这个问题,但我希望你有时间阅读它”。我们非常感谢您确实确实花时间让我们更轻松地为您提供帮助。 -
我不明白为什么人们会发布这类问题。你甚至读过错误吗?你要么不想读它,要么你读了它,但你仍然感到困惑。
-
了解我们大多数人都是自学成才的,包括我自己。