Java 2D数组不会给出总数

时间:2013-12-07 23:58:46

标签: java arrays

如何引入方法以获得所需的结果。我需要获取所有数字的总和,并获得指定行中的最大数字和指定列中的总数。我还需要显示2D数组并使用随机数构建它。我相信整体而言我的代码对每个人都有好处,只需要将所有提示整合在一起吗?

到目前为止,这是我的代码:

import java.util。*;

公共课2darray {

public static int rows=0;
public static int columns=0;

//无法让它工作,但在我需要提交之前已经没时间了。

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int[][] array= new int[rows][columns];



    DisplayArray(array);


    System.out.println("Total of all numbers : " + GetTotal(array));
    System.out.println("Highest in row 2: " + GetHighestInRow(array, 2));
    System.out.println("Column 3 total: " + GetColumnTotal(array, 3));

}

public static void DisplayArray(int[] [] array){


    // this is to display 2D array but couldn't get it to pass into the main

   Random r = new Random();


    int rows = 5;
    int columns=5;

    int [][] ranNums= new int [rows][columns];

    for(int row = 0; row < ranNums.length; row++){
        for(int col =0; col < ranNums[row].length; col++){

            ranNums[row][col]=r.nextInt(20);

        }
    }
    for (int i= 0; i< ranNums.length; i++){// makes the grid
        for (int j=0; j < ranNums[i].length; j++){

            System.out.print(ranNums[i][j] +" ");//prints only 

        }

        System.out.println();

    }

}

public static int GetTotal (int[] []array){
    int total=0;
    int rows=0;
    int columns=0;

    for (int i = 0; i< rows; i++){//increment through the columns and rows
        for (int j=0; j < columns; j++){
            total+=array[i][j]; //we add the to get total
        }
    }
    return total;

}

public static int GetHighestInRow(int [] []array, int sub){//sub is to specify which row

    int columns=0;
    int max = Integer.MIN_VALUE;

    for (int i = 0; i < columns; i++){//increments through the selected row to find highest integer
        if(array[sub][i]>max){
            max = array[sub][i];

        }
    }
    return max;
}

public static int GetColumnTotal (int[] [] array, int sub){//sub is to select column you want total
    int total=0;
    int rows=0;


    for (int i =0; i< rows; i++){
        total+= array[i][sub];
    }
    return total;
}

}

好的,在做了下面列出的一些更改之后,这就是我所拥有的:

import java.util。*;

公共课2darray {

public static int rows=0;
public static int columns=0;


public static void main(String[] args) {
    // TODO Auto-generated method stub

    Random r = new Random();

    int[][] array= new int[rows][columns];

     array [rows][columns]= r.nextInt(10);

    DisplayArray(array);

    System.out.println("Total of all numbers : " + GetTotal(array));
    System.out.println("Highest in row 2: " + GetHighestInRow(array, 2));
    System.out.println("Column 3 total: " + GetColumnTotal(array, 3));

}

public static void DisplayArray(int[] [] array){


    // this is to display 2D array but couldn't get it to pass into the main

//   Random r = new Random();


     rows = 0;
     columns=0;

    int [][] ranNums= new int [rows][columns];

    for(int row = 0; row < ranNums.length; row++){
        for(int col =0; col < ranNums[row].length; col++){

        //  ranNums[row][col]=r.nextInt(20);

        }
    }
    for (int i= 0; i< ranNums.length; i++){// makes the grid
        for (int j=0; j < ranNums[i].length; j++){

            System.out.print(ranNums[i][j] +" ");

        }

        System.out.println();

    }

}

public static int GetTotal (int[] []array){
    int total=0;
     rows=0;
    columns=0;

    for (int i = 0; i< array.length; i++)       
    { int [] thisRow = array [i];
        for (int j=0; j < thisRow.length; j++){
            total+=thisRow[j]; //we add the to get total
        }
    }
    return total;

}

public static int GetHighestInRow(int [] []array, int sub){//sub is to specify which row

    columns=0;
    int max = Integer.MIN_VALUE;

    for (int i = 0; i < columns; i++){//increments through the selected row to find highest integer
        if(array[sub][i]>max){
            max = array[sub][i];

        }
    }
    return max;
}

public static int GetColumnTotal (int[] [] array, int sub){//sub is to select column you want total
    int total=0;
        rows=0;


    for (int i =0; i< rows; i++){
        total+= array[i][sub];//we add the integers in the column to get total
    }
    return total;
}

}

2 个答案:

答案 0 :(得分:1)

这是错误的:

int rows=0;
int columns=0;

for (int i = 0; i< rows; i++){//increment through the columns and rows
    for (int j=0; j < columns; j++) {

这两个循环是空的,因为rowscolumns都是0.同样的错误在你的其他方法中也会重复。

答案 1 :(得分:1)

您永远不会将rowscolumns的值设置为0.将此代码更改为:

public static int GetTotal (int[] []array){
    int total=0;
    int rows=0;
    int columns=0;

    for (int i = 0; i< array.length; i++){//increment through the columns and rows
        int[] thisRow = array[i];
        for (int j=0; j < thisRow.length; j++){
            total+=thisRow[j]; //we add the to get total
        }
    }
    return total;
}