-1

我不确定如何获得所有内容的最大平均值、最小平均值和平均值。我已经在我的代码中为变量设置了值,但是我不确定如何在我的代码中输入平均值。任何帮助都会很棒!谢谢。导入 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);
    }
}
4

2 回答 2

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 来给出平均值。排学生,然后将 avg 与之前的最高和之前的最低平均值进行比较。就像你用分数做的一样!祝你好运。

于 2013-03-02T06:24:49.080 回答
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);
}
}
于 2013-03-04T20:50:41.743 回答