简体   繁体   中英

Averaging the values in a column of a 2D array java

Except for an extra column produced by the code, everything works fine except my avg method which was meant to average the value in each row. I'm new to coding, so maybe I'm not seeing the problem, but the method isn't working as intended. At first I thought it was an issue with the sum but changing it didn't really resolve the initial problem. A column input of (2,1,3) will produce an exception error at 1 but does not occur when the input is (1,3,2). Also the avg is producing only 2 regardless of column length.

I'm aiming for the code to print this when column input of (1,2,3) is entered:

A:2.0 [1.0]

B:2.0 2.0 [2.0]

C:2.0 2.0 2.0 [3.0]

where the bracketed term is the average for that row.

The code:

import java.util.Scanner;
//================================================================
public class ArrayIrreg {
//----------------------------------------------------------------
    private static Scanner Keyboard = new Scanner(System.in);

    public static void main(String[] args) {
    //----------------------------------------------------------------
        char group, rLetter,letter;
        String choice;
        int sum     =  0;
        int num     = 10; // for test
        int rows    = 10;
        int columns =  8;



        // creating 2d array


        System.out.print("Please enter number of rows               : ");
        rows = Keyboard.nextInt();
        Keyboard.nextLine();

        while (rows < 0 || rows >= 10) {
            System.out.print("ERROR:Out of range, try again              : ");
            rows = Keyboard.nextInt();
            Keyboard.nextLine();
        }

        double[][] figures = new double[rows + 1][num];

        for(int t = 0; t < rows; t++) { 
            rLetter = (char)((t)+'A');
            System.out.print("Please enter number of positions in row " + rLetter + " : "); 
                columns = Keyboard.nextInt(); 
                Keyboard.nextLine(); 

            while((columns < 0) || (columns >= 8)) { 
                System.out.print("ERROR:Out of range, try again : "); 
                    columns = Keyboard.nextInt(); 
                    Keyboard.nextLine(); 
            } 

            figures[t] = new double[columns]; 
        }

        // filling the array
        for(int row = 0; row < figures.length; ++row) {
            for(int col = 0; col < figures[row].length; ++col) {
                figures[row][col] = 2.0;
            }
        }

        // printing the array
        for(int row = 0; row < figures.length; ++row) {    
            // printing data row
            group = (char)((row)+(int)'A');
            System.out.print(group + " : ");

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

                System.out.print(figures[row][col] + " ");
                System.out.print(" ");
                }
            System.out.print("["+","+avg(figures)+"]");
            System.out.println();
        }


public static double avg(double temp[][]) {
    int sum = 0;
    int avg = 0;

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

        }

    avg = sum / temp.length;
    return avg;
    }   
}

我认为您做错了,而不是对行中的所有元素求和并取平均值,而是对整个矩阵求和并取平均值,这将始终是相同的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM