最大平均值,最小平均值和平均值

时间:2013-03-02 06:03:27

标签: java average

我不确定如何获得所有内容的最大平均值,最小平均值和平均值。我已经在我的代码中为它们设置了变量值,但是我不知道如何在我的代码中输入平均值。任何帮助都会很棒!谢谢。     import java.util.Scanner;

public class Averages {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int max = 0;
        int min = 9000;
        int score = 0;
        int n;
        int c;
        int rowMaxNumber = 0;
        int rowMinNumber = 0;
        int columnMax = 0;
        int columnMin = 0;
        int averageMin = 0;
        int averageMax = 0;
        double average = 0;

        System.out.println("How many rows?");
        n = scan.nextInt();

        for (int row = 1; row <= n; row++) {
            System.out.println("How many student in row " + row + "?");
            c = scan.nextInt();

            for (int column = 1; column <= c; column++) {
                System.out.println("Score for student " + column + " in row "
                        + row + "?");
                score = scan.nextInt();

                rowMaxNumber = row;
                columnMax = column;

                rowMinNumber = row;
                columnMin = column;

            }
            if (score >= max) {
                max = score;

            }
            if (score < min) {
                min = score;

            }

        }
        System.out.println("Student " + columnMax + " of row " + rowMaxNumber
                + " was highest with " + max);
        System.out.println("Student " + columnMin + " of row " + rowMinNumber
                + " was lowest with " + min);
        System.out.println("Row " + rowMaxNumber + " had highest average with "
                + averageMax);
        System.out.println("Row " + rowMinNumber + " had lowest average with "
                + averageMax);
        System.out.println("Class average is " + average);
    }
}

2 个答案:

答案 0 :(得分:0)

public class Averages {
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int max = 0;
    int min = 9000;
    int score = 0;
    int n;
    int c;
    int rowsum = 0;
    int rowmaxnumber = 0;
    int rowminnumber = 0;
    int columnmax = 0;
    int columnmin = 0;
    int averagemin = 9999;
    int averagemax = 0;
    double average = 0;

    System.out.println("How many rows?");
    n = scan.nextInt();

    for (int row = 1; row <= n; row++) {
        System.out.println("How many student in row " + row + "?");
        c = scan.nextInt();
        rowsum = 0;
        for (int column = 1; column <= c; column++) {

            System.out.println("Score for student " + column + " in row "
                    + row + "?");
            score = scan.nextInt();
            rowsum += score;
            rowmaxnumber = row;
            columnmax = column;

            rowminnumber = row;
            columnmin = column;

            if (score >= max) {
                max = score;
            }
            if (score < min) {
                min = score;
            }

        }
        int avg=rowsum/c;
        if( avg >= averagemax){
            averagemax=avg;
        }
        if( avg <= averagemin){
            averagemin=avg;
        }




    }
    System.out.println("Student " + columnmax + " of row " + rowmaxnumber
            + " was highest with " + max);
    System.out.println("Student " + columnmin + " of row " + rowminnumber
            + " was lowest with " + min);
    System.out.println("Row " + rowmaxnumber + " had highest average with "
            + averagemax);
    System.out.println("Row " + rowminnumber + " had lowest average with "
            + averagemin);
    System.out.println("Class average is " + average);
}

}

我已经重新构建了一些代码,以便在我们连续看到它们时计算得分总和,并计算扫描行末尾的平均值,avg也会通过将rowsum除以no得到平均值。排成一排的学生,然后将平均成绩与之前的最高和之前的最低平均成绩进行比较。就像你用得分一样!祝你好运。

答案 1 :(得分:0)

我想出了如何获得最大平均值,最小平均值和总平均值。感谢任何帮助过的人!

import java.util.Scanner;

public class Averages 
{
 public static void main ( String[] args )
  {
     Scanner scan = new Scanner(System.in);
        int max = -9999;
        int min = 9999;
        int score = 0;
        int n;
        int c;
        int rowsum = 0;
        int rowMaxNumber = 0;
        int rowMinNumber = 0;
        int columnMax = 0;
        int columnMin = 0;
        double averageMin = 9999;
        double averageMax = 0;
        double avg = 0.0;
        double avgTotal = 0.0;
        double totalScore = 0;
        double totalStudent = 0;            

        System.out.println("How many rows?");
        n = scan.nextInt();

        for (int row = 1; row <= n; row++) {
            System.out.println("How many student in row " + row + "?");
            rowsum=0;
            c = scan.nextInt();
            totalStudent += c;

            for (int column = 1; column <= c; column++) {

                System.out.println("Score for student " + column + " in row " + row + "?");
                score = scan.nextInt();
                rowsum += score;
                totalScore += score;     

                if (score >= max) {
                    max = score;
                    columnMax = column;

                }
                if (score < min) {
                    min = score;
                    columnMin = column;
                }
            }
            avg = rowsum/c;
            if( avg >= averageMax){
                averageMax=avg;
                rowMaxNumber = row;

            }
            if( avg < averageMin){
                averageMin=avg;
                rowMinNumber = row;
            }
        }
        avgTotal = totalScore/totalStudent;

        System.out.println("Student " + columnMax + " of row " + rowMaxNumber + " was highest with " + max);
        System.out.println("Student " + columnMin + " of row " + rowMinNumber + " was lowest with " + min);
        System.out.println("Row " + rowMaxNumber + " had highest average with " + averageMax);
        System.out.println("Row " + rowMinNumber + " had lowest average with " + averageMin);
        System.out.println("Class average is " + avgTotal);
}
}