程序计算错误的平均值

时间:2014-01-08 18:17:10

标签: java

该程序应允许多个用户(学生)输入多个科目的考试和项目分数,然后分别输出平均分数,然后根据项目分数的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

4 个答案:

答案 0 :(得分: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 :(得分:0)

sum1 = sum1 + exam1;

你在循环中加入sum1,但在转到另一个学生之后永远不会将sum1重置为0。通过所有学生在循环内初始化你的总和,或者只在该循环的开头给它指定0。

答案 2 :(得分:0)

您的sumsum1积累了所有分数和所有学生。你可能想要在循环中迭代所有学生。

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

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

答案 3 :(得分: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;