gpt4 book ai didi

JAVA二维数组代码

转载 作者:行者123 更新时间:2023-12-01 18:35:13 25 4
gpt4 key购买 nike

我想编写一个程序来创建一个用测试数据初始化的二维 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));
}
}

最佳答案

我看到一个大错误:

你可以这样解决:

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;
}
  1. x 和 y 未在您的代码中初始化
  2. 为什么除以x+y。您需要按元素计数进行除法。

关于JAVA二维数组代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22287524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com