2014-01-08 43 views
1

该程序应允许多个用户(学生)进入他的考试和多个科目的项目分数,然后分别输出平均分数,然后计算基于20%的项目分数的总和平均值和80%的考试。程序计算不正确的平均值

现在的问题是第一个学生的平均结果是正确的,但程序循环到下一个学生的平均值是不正确的。

import java.util.Scanner; 

public class Averages4{ 

public static void main(String[] args) { 


String name1; 
int sub, counter, num1; 


double project = 0.0, exam1 = 0.0, avg = 0.0,avg1 = 0.0,sum = 0.0,avgall = 0.0,sum1 = 0.0; 


Scanner inputStudent = new Scanner(System.in); 

System.out.println("Enter the number of students to record scores then press enter: "); 
    int student = inputStudent.nextInt(); 
     for (int z = 1; z <= student; z++) //this loops the entire program based on the the number of students. 
     { 

      Scanner inputFirstname = new Scanner(System.in); 
      System.out.println("\nPlease enter your first name"); 
      name1 = inputFirstname.nextLine(); 

      System.out.println("First name: "+ name1); 



      System.out.print("MENU TO CHOOSE FROM:\n");  

      System.out.println("1.Spanish \n 2. Biology\n 3. Chemistry \n" 
      + " 4.Physics\n"); 


      Scanner inputRegnum1 = new Scanner(System.in); 
      System.out.println("How many subjects do you what to calculate scores for? " 
        + "Please enter.\n "); 
      counter = inputRegnum1.nextInt(); 

        { 
        for (int x = 1; x <= counter; x++) /*This for loop keeps a count on the 
                  number of subjects chosen.*/ 
        { 
         System.out.println("Choose your subject(s)\n"); 
         Scanner test3 = new Scanner(System.in); 
         sub = test3.nextInt(); 
          System.out.println(" "); 

         //switch used to select subjects. 
         switch (sub) { 
         case 1: System.out.println("Subject: 1. Spanish");break; 
         case 2: System.out.println("Subject: 2. Biology");break; 
         case 3: System.out.println("Subject: 3. Chemistry");break; 
         case 4: System.out.println("Subject: 4. Phyics");break; 
         default: System.out.println("Invalid option. Please choose option 1 - 4");break; 
            } 

          } 
          Scanner test2 = new Scanner(System.in); 
          System.out.println("\n\nHow many averages should I calculate for each subject?"); 

          num1 = test2.nextInt();  


          Scanner test = new Scanner(System.in); 
          System.out.println("Enter each of the "+ num1 + " project scores for the " +counter+" subject(s) and press enter\n"); 

          //This for loop keeps a count on the total number of project scores. 
          for (int y = 1; y <= num1; y++){ 
          project = test.nextDouble(); 
          sum = sum + project; // calculates the total project score sums. 

          //outputs the project scores.); 
          System.out.println("The project score recorded is "+project); 
          } 

         avg = sum/num1; //calculates the project average. 
         System.out.println("\nThe project average recorded is " + avg); 
         } 


         Scanner test1 = new Scanner(System.in); 
         System.out.println("Enter each of the "+ num1 + " examination scores for the " +counter+" subject(s) and press enter\n"); 

         for (int y = 1; y <= num1; y++){ //keep count of the number of assignments. 

         exam1 = test1.nextDouble(); 
         sum1 = sum1 + exam1; 

         System.out.println("\nThe examination score recorded is "+exam1); 
         } 


         avg1 = sum1/num1;//calculates the project average. 

         System.out.println("\nThe examination average recorded is " + avg1); 


         avgall = (avg*.2) + (avg1 * .8);//calculates the overall average. 
         System.out.println("\nThe overall average is "+ avgall);//outputs the overall average. 



         System.out.println("\n Average project score: "+ avg); 
         System.out.println("\n Average Exam score: "+ avg1); 
         System.out.println("\n Average Overall score: "+ avgall); 


       } 

       } 
      } 

实例:结果

First student – results for 2 subjects 
    Project score recorded is: 65.0 
    Project score recorded is: 70.0 
    Average project score: 67.5 

The examination score recorded is 55.0 
The examination score recorded is 60.0 
Average Exam score: 57.5 
Average Overall score: 59.5 



Second student – results for 2 subjects 
Incorrect results. 

The project score recorded is 50.0 
The project score recorded is 55.0 
Average project score: 120.0 

The examination score recorded is 45.0 
The examination score recorded is 50.0 

Average Exam score: 105.0 
Average Overall score: 108.0 
+2

尝试调试,您​​可能没有正确清除变量。 – Taylor

+2

您还应列出您试图解决问题的方法,而不仅仅是代码/问题 – nevets1219

+0

快速浏览一下@Taylor所说的内容,试着声明/初始化for循环中的变量,以便每个学生都能获得“他们自己的”测试结果。 – cYn

回答

0

看起来你需要在第二个循环中“清零”所有东西。它似乎工作,当我这样做。重点在似乎,因为它很难遵循程序的流程。试试看看会发生什么

for (int x = 1; x <= counter; x++) /* 
            * This for loop keeps a 
            * count on the number of 
            * subjects chosen. 
            */ 

{ 

    project = 0.0; exam1 = 0.0; avg = 0.0; avg1 = 0.0; sum = 0.0; avgall = 0.0; sum1 = 0.0; 
+0

谢谢@peekillet,我非常感谢你的帮助。 – user3171620

0

如果采取不正确的平均水平(120),并比较正确平均应该是什么(52.5),你很快就会发现,不同的是什么但是以前的学生的平均水平(67.5)。

考虑到这一发现,问题就显而易见了。用于跟踪每个学生平均水平的变量不会被重新初始化为每个新生的零。更大的问题实际上是变量的范围不正确。

为什么要在所有学生操作的方法顶部定义保持变量,这些变量是针对学生的?

//int studentTotal = 0; //Don't define it here. 
for each student { 
    int studentTotal = 0; //Define variables in the narrowest scope 
    for each score { 
     studentTotal = studentTotal + score; 
    } 
} 
+1

谢谢@凤凰,我非常感谢你的帮助。 – user3171620

0
sum1 = sum1 + exam1; 

您添加到您的SUM1环内,但从来没有复位SUM1将另一名学生后为0。在循环中初始化你的总和,通过所有的学生,或者只在该循环的开始分配0。

+0

谢谢@Byakuya,我意识到 - 很大的帮助! – user3171620

0

您的sumsum1正在累积所有分数和所有学生。你会想要在循环内循环遍历所有的学生。

for (int z = 1; z <= student; z++) 
    { 
    sum = 0.0; 
    sum1 = 0.0; 

    Scanner inputFirstname = new Scanner(System.in); 
... 
+1

谢谢@PopoFibo,我意识到分数正在累积。再次感谢。 – user3171620