Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

java - Error returning the wrong value for the max average of an two dimension array

This is a program that takes an two dimensions array that have students mark on the columns and different students on each row, when I'm running the program I'm having problems:

1- I must enter each student score, so I enter numbers and the program keep on running even if I hit the array bound limits, However when I tired to Initialize the two dimension array by making a count var and assigning it each array index this happens.

2- making the two dimension array have numbers from 0 to 14, I get the correct Average of all rows, but the Max Average is returning 00.

import javax.swing.*;
import java.util.*;
public class JavaTest {
    public static void main(String[] args) {
        Engineer obj = new Engineer();
        int[][] array = new int[5][3];
        Scanner input = new Scanner(System.in);// or adding int count = 0; here
        for (int i = 0; i < 5; i++){
            for (int j =0; j<3; j++){
                array[i][j] = input.nextInt(); // and making array[i][j] = count ++;
            }
        }
        obj.ComputeAvg(array);
        JOptionPane.showMessageDialog(null, obj.getAvg() + "" + obj.getImaxAvg());
    }

    static class Engineer {
        private final int maxAvg, ImaxAvg;

        Engineer() {
            maxAvg = ImaxAvg = 0;
        }

        Engineer(int mm, int ii) {
            maxAvg = mm;
            ImaxAvg = ii;
        }

        public int getAvg() {
            return maxAvg;
        }

        public int getImaxAvg() {
            return ImaxAvg;
        }

        public void ComputeAvg(int[][] array) {
            int scores;
            int total;
            int[] scores_array = new int[5];
            for (int i = 0; i < 5; i++) {
                total = 0;
                for (int j = 0; j < 3; j++) {
                    total += array[i][j];
                }
                scores = total / 3;
                scores_array[i] = scores;
            }

            StringBuilder score = new StringBuilder("THe output is");
            for (int j : scores_array) {
                score.append(j).append(" ");
            }
            JOptionPane.showMessageDialog(null, score.toString());
            ComputeMax(scores_array);
        }

        public void ComputeMax(int[] array) {
            int maxvar = array[0];
            int imaxvar = 0;
            for (int i = 0; i < array.length; i++) {
                if (array[i] > maxvar) {
                    maxvar = array[i];
                    imaxvar = i;
                }
            }
            new Engineer(maxvar, imaxvar);
        }
    }
}
question from:https://stackoverflow.com/questions/65599759/error-returning-the-wrong-value-for-the-max-average-of-an-two-dimension-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...