[英]JAVA two-dimensional array code
我想編寫一個程序,創建一個用測試數據初始化的二維int數組。 該程序無法運行。 我對代碼感到困惑,請幫助解決問題。如何更正我的代碼? 謝謝。
public class Int2DArray {
private static int x;
private static int y;
public static int getTotal(int[][] numbers) {
int total = 0;
for (int x = 0; x < numbers.length; x++);
for (int y = 0; y < numbers[x].length; y++);
total = total + numbers[x][y];
return total;
}
public static double getAverage(int[][] numbers) {
double average = 0;
average = getTotal(numbers) / (x + y);
return average;
}
public static int getRowTotal(int[][] numbers, int index) {
int total = 0;
for (int y = 0; y < 3; y++) {
total = total + numbers[index][y];
}
return total;
}
public static int getColumnTotal(int[][] numbers, int index) {
int total = 0;
for (int x = 0; x < numbers.length; x++) {
total = total + numbers[x][index];
}
return total;
}
public static int getHighestInRow(int[][] numbers, int x) {
int high = numbers[x][0];
for (int i = 1; i < 3; i++) {
if (numbers[x][i] > high) {
high = numbers[x][i];
}
}
return high;
}
public static int getLowestInRow(int[][] numbers, int x) {
int low = numbers[x][0];
for (int i = 1; i < 3; i++) {
if (numbers[x][i] < low) {
low = numbers[x][i];
}
}
return low;
}
}
public class Int2DArrayTest {
public static void main(String[] args) {
int[][] iarray = {{2, 1, 9}, {7, 3, 4}, {5, 6, 8}};
System.out.println("Total:" + getTotal(iarray));
System.out.println("Average:" + getAverage(iarray));
System.out.println("Total of row 0" + getRowTotal(iarray, 0));
System.out.println("Total of row 1" + getRowTotal(iarray, 1));
System.out.println("Total of row 2" + getRowTotal(iarray, 2));
System.out.println("Total of col 0" + getColumnTotal(iarray, 0));
System.out.println("Total of col 1" + getColumnTotal(iarray, 1));
System.out.println("Total of col 2" + getColumnTotal(iarray, 2));
System.out.println("Highest in row 0" + getHighestInRow(iarray, 0));
System.out.println("Highest in row 1" + getHighestInRow(iarray, 1));
System.out.println("Highest in row 2" + getHighestInRow(iarray, 2));
System.out.println("Lowest in row 0" + getLowestInRow(iarray, 0));
System.out.println("Lowest in row 1" + getLowestInRow(iarray, 1));
System.out.println("Lowest in row 2" + getLowestInRow(iarray, 2));
}
}
您已將Int2DArray
內部的方法Int2DArray
為靜態
public static int getTotal(int[][] numbers) { //
因此,您需要將它們稱為靜態,以便像下面這樣使用它們:
System.out.println("Total:" + Int2DArray.getTotal(iarray));
我看到一個大錯誤:
您可以這樣解決:
public static int getTotal(int[][] numbers) {
int total = 0;
for (int x = 0; x < numbers.length; x++);
for (int y = 0; y < numbers[x].length; y++);
total = total + numbers[x][y];
return total;
}
代替
public static int getTotal(int[][] numbers) {
int total = 0;
for (int x = 0; x < numbers.length; x++){
for (int y = 0; y < numbers[x].length; y++){
total = total + numbers[x][y];
}
}
return total;
}
或者更好(使用foreach):
public static int getTotal(int[][] numbers) {
int total = 0;
for (int [] x : numbers){
for (int y : x){
total = total + y;
}
}
return total;
}
並在您的IDE中使用格式化熱鍵。
public static int getRowTotal(int[][] numbers, int index) {
int total = 0;
for (int y = 0; y < 3; y++) {
total = total + numbers[index][y];
}
return total;
}
在這里您使用常量3-這是不好的樣式,您需要將其提取為常量
public static double getAverage(int[][] numbers) {
double average = 0;
average = getTotal(numbers) / (x + y);
return average;
}
x+y
。 您需要對元素數量進行划分。 您有兩次以下行,因此是一個明顯的語法錯誤。
public class Int2DArrayTest {
此外,getAverage函數中也存在邏輯錯誤。
average = getTotal(numbers) / (x + y);
它顯示了被零除的異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.