Java 计算学生每次考试和评分的平均分数
我的程序有一些问题,因为它无法计算每次考试的平均分数,以及学生的平均分数和分数 有人能帮我改正我的代码吗?因为我需要在星期三交作业。还有,帮我检查里面的其他代码有错误 非常感谢Java 计算学生每次考试和评分的平均分数,java,Java,我的程序有一些问题,因为它无法计算每次考试的平均分数,以及学生的平均分数和分数 有人能帮我改正我的代码吗?因为我需要在星期三交作业。还有,帮我检查里面的其他代码有错误 非常感谢 import java.util.*; import java.io.*; public class Assignment { public static void main(String[] args) throws IOException { // Variables for input name and
import java.util.*;
import java.io.*;
public class Assignment {
public static void main(String[] args) throws IOException {
// Variables for input name and output name
String nameInp = "";
String nameOut = "";
// Arrays to store marks of 5 tests of each student
double [] tests = new double[5];
// Arrays to store average and sum of the 5 tests
double[] testAvg = new double[5];
double[] testSum = new double[5];
// Arrays to store max and min of the 5 tests
double[] max = new double[5];
double[] min = new double[5];
// Arrays to store the name of students who got max or min
// in the 5 tests
String[] nameMax = new String[5];
String[] nameMin = new String[5];
// Number of students
int studCount = 0;
// Average and grade of the 5 tests of each students
double studAverage;
String studGrade;
// Scanner for reading file
Scanner scf = new Scanner(new File(System.getProperty("user.dir") +
"/src/student_marks.txt"));
// Set the max and min of the 5 tests
// to Double's min and max respectively
double max_tests = Double.MAX_VALUE;
double min_tests = Double.MIN_VALUE;
// Print output heading
System.out.println("Student Name Test1 Test2 Test3 Test4 Test5 Average Grade");
System.out.println("---------------------------------------------------------");
// While there are more data to read from the input file
while (scf.hasNext()) {
// Increment number of students
int count;
count++;
// Read a student name
nameInp = scf.next();
// Replace the "_" in the name with a " "
nameOut = nameInp.replaceAll("_", " ");
// For each student, calculate the sum of the test marks,
// record the max and min marks of each test and
tests[5] = scf.nextDouble();
testSum[5] += tests[5];
if (tests[5] > max_tests) {
max_tests = tests[5];
}
if (tests[5] < min_tests) {
min_tests = tests[5];
}
// record the name of studunts scoring those max and min marks
if (tests[5] > max_tests) {
max_tests = tests[5];
nameMax[5] = nameOut;
}
if (tests[5] < min_tests) {
min_tests = tests[5];
nameMin[5] = nameOut;
}
// Call the computeStudAvg method to calculate the average marks
// of each student
studAverage = computeStudAvg( testAvg );
// Call the computeStudGrade to get the grade of each student
studGrade = computeStudGrade( );
// Print the tests, average and grade of each student
System.out.printf("%-10s %6.2f %6.2f %6.2f\n", nameOut, tests[1], tests[2], tests[3], tests[4], tests[5], studGrade);
} // end while more students
// Calculate the average marks of each of the 5 tests
testAvg[5] = testSum[5] / 5;
// Print the average of each test
System.out.printf("Test Average: ", testAvg[1], testAvg[2], testAvg[3], testAvg[4], testAvg[5]);
// Print the max marks, min marks and names of students
System.out.println("Test Max--Student Min-Student ");
System.out.println("------------------------------------------------------------------");
System.out.printf("Test1" + max[1] , nameMax[1] , min[1] , nameMin[1]);
System.out.printf("Test2" + max[2] , nameMax[2] , min[2] , nameMin[2]);
System.out.printf("Test3" + max[3] , nameMax[3] , min[3] , nameMin[3]);
System.out.printf("Test4" + max[4] , nameMax[4] , min[4] , nameMin[4]);
System.out.printf("Test5" + max[5] , nameMax[5] , min[5] , nameMin[5]);
System.out.println();
scf.close();
}
// Method to compute and return the average mark of the 5 tests for each student
public static double computeStudAvg(double[] testSum) {
return (testSum[5] / 5 );
}
// Method to compute and return the grade of each student
public static String computeStudGrade(double[] studAvg ) {
if ( average >=75 ) {
return "A";
} else if ( average >= 65 ) {
return "B";
} else if ( average >= 50 ) {
return "C";
} else if ( average >= 40 ) {
return "D";
} else
return "F";
}
}
import java.util.*;
导入java.io.*;
公共课堂作业{
公共静态void main(字符串[]args)引发IOException{
//输入名称和输出名称的变量
字符串nameInp=“”;
字符串nameOut=“”;
//数组以存储每个学生5次测试的分数
双[]测试=新双[5];
//存储5个测试的平均值和总和的数组
double[]testAvg=新的double[5];
double[]testSum=新的double[5];
//存储5个测试的最大值和最小值的数组
双精度[]最大值=新双精度[5];
双精度[]最小值=新双精度[5];
//数组以存储获得最大值或最小值的学生的姓名
//在5次测试中
字符串[]nameMax=新字符串[5];
字符串[]nameMin=新字符串[5];
//学生人数
int studCount=0;
//每个学生5次考试的平均分和成绩
双平均;
串级;
//用于读取文件的扫描仪
Scanner scf=new Scanner(新文件(System.getProperty(“user.dir”)+
“/src/student_marks.txt”);
//设置5个测试的最大值和最小值
//分别加倍的最小值和最大值
double max_测试=double.max_值;
双最小值试验=双最小值;
//打印输出标题
System.out.println(“学名Test1 Test2 Test3 Test4 Test5平均成绩”);
System.out.println(“------------------------------------------------------------------”;
//虽然有更多的数据要从输入文件中读取
while(scf.hasNext()){
//增加学生人数
整数计数;
计数++;
//读一个学生的名字
nameInp=scf.next();
//将名称中的“\”替换为“”
nameOut=nameInp.replaceAll(““,”);
//对于每个学生,计算考试分数的总和,
//记录每次测试和测试的最大和最小标记
测试[5]=scf.nextDouble();
testSum[5]+=测试[5];
如果(试验[5]>最大试验){
最大试验=试验[5];
}
如果(试验[5]<最小试验){
最小试验=试验[5];
}
//记录得分最高和最低的学生的姓名
如果(试验[5]>最大试验){
最大试验=试验[5];
nameMax[5]=nameOut;
}
如果(试验[5]<最小试验){
最小试验=试验[5];
nameMin[5]=nameOut;
}
//调用computeStudAvg方法来计算平均分数
//每个学生的
studAverage=computeStudAvg(testAvg);
//调用computeStudGrade以获取每个学生的分数
studGrade=computeStudGrade();
//打印每个学生的考试、平均成绩和分数
System.out.printf(“%10s%6.2f%6.2f%6.2f%6.2f\n”,命名、测试[1]、测试[2]、测试[3]、测试[4]、测试[5]、等级);
}//当更多的学生
//计算5次测试的平均分数
testAvg[5]=testSum[5]/5;
//打印每个测试的平均值
System.out.printf(“测试平均值:”,testAvg[1],testAvg[2],testAvg[3],testAvg[4],testAvg[5]);
//打印学生的最高分数、最低分数和姓名
System.out.println(“测试最大值——学生最小值”);
System.out.println(“---------------------------------------------------------------------------”);
System.out.printf(“Test1”+max[1],nameMax[1],min[1],nameMin[1]);
System.out.printf(“Test2”+max[2],nameMax[2],min[2],nameMin[2]);
System.out.printf(“Test3”+max[3],nameMax[3],min[3],nameMin[3]);
System.out.printf(“Test4”+max[4],nameMax[4],min[4],nameMin[4]);
System.out.printf(“Test5”+max[5],nameMax[5],min[5],nameMin[5]);
System.out.println();
scf.close();
}
//方法计算并返回每个学生5次测试的平均分数
公共静态双计算UDAVG(双[]测试和){
返回(testSum[5]/5);
}
//方法计算并返回每个学生的成绩
公共静态字符串computeStudGrade(双[]studAvg){
如果(平均值>=75){
返回“A”;
}否则,如果(平均值>=65){
返回“B”;
}否则,如果(平均值>=50){
返回“C”;
}否则,如果(平均值>=40){
返回“D”;
}否则
返回“F”;
}
}
您的问题之一是计算最小值和最大值。颠倒这些行中的赋值:
double max_tests = Double.MAX_VALUE;
double min_tests = Double.MIN_VALUE;
应该是:
double max_tests = Double.MIN_VALUE;
double min_tests = Double.MAX_VALUE;
当您将分数与
min\u测试进行比较时,如果它的值为Double.min\u value
,则比较将始终失败,因为没有任何分数小于Double.min\u value
Stackoverflow不是作业解决方案中心。至少你应该确切地指定你的程序应该做什么。看起来你的代码中有几个问题<代码>整数计数代码>这需要在循环外声明。您在访问数组元素时没有正确使用索引,而是硬编码。某些部分存在需要解决的编译问题。