2013-03-02 297 views
-1

我不確定如何獲得最大平均值,最小平均值和平均值。我已經在代碼中爲它們設置了變量值,但是我不知道如何在代碼中輸入平均值。任何幫助將是偉大的!謝謝。 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); 
    } 
} 
+0

我可能會爲您的變量增加一些清晰度 - 「row_max_number」或「rowMaxNumber」。他們很難閱讀。 – ChiefTwoPencils 2013-03-02 06:05:45

+0

對不起,我做了你建議的改變,使其更易於閱讀。 – 2013-03-02 06:09:42

回答

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); 
} 
} 
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); 
} 

}

我已經重構了一些代碼來計算分數的總和,因爲我們連續看到他們,並計算在掃描行的結尾平均,也平均會給被平均將行劃分爲no。的學生排隊,然後將平均值與先前的最高和最低平均值進行比較。就像你做分數一樣!祝你好運。

+0

謝謝,我想出瞭如何獲得平均值併發布答案,謝謝你的幫助! – 2013-03-04 20:52:12

相關問題