2014-09-27 90 views
0

我已經能夠創建一個程序來計算每個學生進入的字母等級和平均值,但我很難嘗試創建一個記錄他們都給我的最後一個計數器平均一班。這是我整個計劃中最困難的部分。我不明白如何實施櫃檯,我需要幫助。需要計算和平均finalaverage輸出

import java.util.Scanner; 
public class gradebook { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 


     float discussionweight, hwweight, examweight, discussion, hw, exam, finalaverage; 
     Scanner scan = new Scanner(System.in); 
     System.out.print ("Enter the weight of the discussions as an integer: "); 
     discussionweight = scan.nextFloat(); 
     // Prompts for the weight of the homework grade in an integer 
       System.out.print ("Enter the weight of the homework as an integer: "); 
       hwweight = scan.nextFloat(); 
       System.out.print("Enter the weight of the exam as an integer:"); 
       examweight = scan.nextFloat(); 
     int again=-1; 


     while(again!=0){ 


     System.out.print("Enter the students name: "); 
     String student = scan.next(); 


     // Prompts for the discussions grade 
     System.out.print ("Enter the score of the discussion as an integer: "); 
     discussion = scan.nextFloat(); 



     // Prompts for hw grade 
     System.out.print ("Enter the hw grade: "); 
     hw = scan.nextFloat(); 

     System.out.print("Enter the exam grade"); 
     exam = scan.nextFloat(); 
     finalaverage = (((discussionweight * discussion) + (hw * hwweight) + (exam * examweight))/100);  
     if (finalaverage >= 90) 
     System.out.println(student +"'s " + "final grade is an A."); 
     else if (finalaverage >= 80) 
     System.out.println(student +"'s " + "final grade is a B."); 
     else if (finalaverage >= 70) 
     System.out.println(student +"'s " + "final grade is a C."); 
     else if (finalaverage >= 60) 
     System.out.println(student +"'s " + "final grade is a D."); 
     else if (finalaverage >= 10) 
     System.out.println(student +"'s " + "final grade is an F."); 

     System.out.println ("The final average is "+ finalaverage); 
     System.out.print("Would you like to continue? Enter 0 to exit or enter 1 to continue."); 
     again = scan.nextInt();} 

     System.out.print("End Gradebook"); 

     float integer; 

     double sum = 0; 
     double average = 0; 

     int count = 0; 

     // set integer = to the nextInt() while looping so it calculates properly 
     while ((integer = ((finalaverage)) != 0) { 
      count ++; 
      sum += integer; 
     } 
     average = sum/count; // calculate the average after the while-loop 

     System.out.println("Average = " + average); 

    } 
    } 

回答

0
while(again!=0){ 

我認爲這個循環重複爲每個學生。這意味着你想在這個循環內增加一次的學生數量。然而,你有

while ((integer = ((finalaverage)) != 0) { 
     count ++; 
     sum += integer; 
    } 

這可能會增加學生數量爲每個學生幾次。

爲了解決這個問題,你應該能夠簡單地移除這個嵌套的while循環。只需增加學生人數並將當前學生的成績添加到班級總數中即可。

只有在完成循環後才能計算出班級的平均值。這意味着average = sum/count;行應該是以外的這個循環。

p.s.我建議修改你的變量名稱。由於它被宣佈爲float,因此它不是非常具有描述性和完全誤導性。事實上,我認爲你甚至不需要這個integer變量。 sumcount稍微好一些,但可以更改爲classSumstudentCount以更具描述性。

0

不,不需要第二次循環。你需要的是增加和每一個環帶finalaverage和增量次數每個循環

while(again!=0){ 
... 
sum += finalaverage; 
count++; 
} 

完整的源代碼是這樣的:

public static void main(String[] args) { 
     float discussionweight, hwweight, examweight, discussion, hw, exam, finalaverage = 0; 
     Scanner scan = new Scanner(System.in); 
     System.out.print("Enter the weight of the discussions as an integer: "); 
     discussionweight = scan.nextFloat(); 
     // Prompts for the weight of the homework grade in an integer 
     System.out.print("Enter the weight of the homework as an integer: "); 
     hwweight = scan.nextFloat(); 
     System.out.print("Enter the weight of the exam as an integer:"); 
     examweight = scan.nextFloat(); 
     int again = -1; 

     int count = 0, sum = 0; 

     while (again != 0) { 
      count++; 

      System.out.print("Enter the students name: "); 
      String student = scan.next(); 

      // Prompts for the discussions grade 
      System.out.print("Enter the score of the discussion as an integer: "); 
      discussion = scan.nextFloat(); 

      // Prompts for hw grade 
      System.out.print("Enter the hw grade: "); 
      hw = scan.nextFloat(); 

      System.out.print("Enter the exam grade"); 
      exam = scan.nextFloat(); 
      finalaverage = (((discussionweight * discussion) + (hw * hwweight) + (exam * examweight))/100); 
      if (finalaverage >= 90) { 
       System.out.println(student + "'s " + "final grade is an A."); 
      } else if (finalaverage >= 80) { 
       System.out.println(student + "'s " + "final grade is a B."); 
      } else if (finalaverage >= 70) { 
       System.out.println(student + "'s " + "final grade is a C."); 
      } else if (finalaverage >= 60) { 
       System.out.println(student + "'s " + "final grade is a D."); 
      } else if (finalaverage >= 10) { 
       System.out.println(student + "'s " + "final grade is an F."); 
      } 

      System.out.println("The final average is " + finalaverage); 

      sum += finalaverage; 

      System.out.print("Would you like to continue? Enter 0 to exit or enter 1 to continue."); 
      again = scan.nextInt(); 
     } 

     System.out.print("End Gradebook"); 

     // set integer = to the nextInt() while looping so it calculates properly 
     int average = sum/count; // calculate the average after the while-loop 

     System.out.println("Average = " + average); 

    }