【发布时间】:2015-12-19 04:11:59
【问题描述】:
我刚刚完成了一个在二维数组中查找最小 int 然后返回它的程序。我想返回最小的 int 然后在 main 中打印它。我只是不知道如何调用 findSmallest 方法,所以我可以在 main.js 中打印它。任何帮助将不胜感激。
这是我的代码:
public class FinalQ1 {
public int[][] createArray(int rSize, int cSize) {
Random r = new Random();
int[][] array = new int[rSize][cSize];
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[0].length; col++) {
array[row][col] = r.nextInt(101);
}
}
return array;
}
public int findSmallest(int[][] Array) {
int[][] coord = Array;
int smallest = Integer.MAX_VALUE;
for (int i = 0; i < coord.length; i++) {
for (int j = 0; j < coord.length; i++) {
if (smallest > coord[i][j]) {
smallest = coord[i][j];
}
}
}
return smallest;
}
public static void main(String[] args) {
FinalQ1 c = new FinalQ1();
int small;
small = c.findSmallest();
}
}
【问题讨论】:
-
System.out.println(small);同样,你需要一个j++在你的内部 for 循环(不是i++)。 -
findSmallest()需要二维数组 -
看起来你会想要使用 createArray 首先创建你的二维数组。
System.out.println(c.findSmallest(createArray(2, 2));将一举搞定 -
@ElliottFrisch 哎呀哈哈没有注意到这一点。谢谢!