我不確定如何獲得最大平均值,最小平均值和平均值。我已經在代碼中爲它們設置了變量值,但是我不知道如何在代碼中輸入平均值。任何幫助將是偉大的!謝謝。 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);
}
}
我可能會爲您的變量增加一些清晰度 - 「row_max_number」或「rowMaxNumber」。他們很難閱讀。 – ChiefTwoPencils 2013-03-02 06:05:45
對不起,我做了你建議的改變,使其更易於閱讀。 – 2013-03-02 06:09:42