该程序应允许多个用户(学生)进入他的考试和多个科目的项目分数,然后分别输出平均分数,然后计算基于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
尝试调试,您可能没有正确清除变量。 – Taylor
您还应列出您试图解决问题的方法,而不仅仅是代码/问题 – nevets1219
快速浏览一下@Taylor所说的内容,试着声明/初始化for循环中的变量,以便每个学生都能获得“他们自己的”测试结果。 – cYn