我创建此代码来平均测试分数,但是我将每个学生test1,test2,test3等放在一个数组中。现在我怎么能得到一个测试的测试分数的总体平均值(例如q1在数组索引0中,但它们是8个不同的q1。我如何获得它们的平均值?)
import java.io.File;
import java.io.IOException;
import java.util.*;
public class Project3 {
public static void main(String[] args) throws IOException {
ReadFile r = new ReadFile();
r.openFile();
r.readFile();
r.closeFile();
-
import java.io.*;
import java.lang.*;
import java.util.*;
public class Grades {
private Formatter x;
//Formatter variable- output string to file
public void openFile(){
//method to open file
try{
x = new Formatter("grades.txt");
}
catch(Exception e){
System.out.println("Could not find file.");
}
}
public void addRecords(){
x.format("%d",8);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", " Thui", "Bhu", 100, 90, 80, 100, 89, 99, 88);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n ArianaB", "Smith", 90, 90, 100, 100, 99, 100, 95);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Emily", "Gonzales", 100, 90, 100, 70, 78, 78, 80);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Jenifer", "L", 80, 90, 90, 100, 89, 99, 85);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Maria", "Jones", 65, 72, 77, 68, 62, 70, 65);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Bill", "Gates", 60, 54, 38, 62, 65, 60, 50);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Escobar", "Morris", 83, 77, 88, 76, 79, 72, 76 );
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Anne", "Latner", 80, 90, 85, 95, 90, 95, 90 );
}
public void closeFile(){
x.close();
}
//
}
-
import java.io.*;
import java.util.InputMismatchException;
import java.util.*;
public class ReadFile {
private Scanner x;
public void openFile(){
try{
x = new Scanner (new File ("grades.txt"));
}
catch(Exception e){
System.out.println("could not find file");
}}
public void createFile()throws IOException {
try{
File file = new File("grades.txt");
Scanner s = new Scanner(file);
while(s.hasNext()){
{
double q1average = 0.0, q2average = 0.0, q3average = 0.0, q4average = 0.0;
double proji = 0.0, projii =0.0, projiii = 0.0;
double q1sum = 0.0, q2sum = 0.0, q3sum = 0.0, q4sum = 0.0;
double proji_sum = 0.0, projii_sum = 0.0, projiii_sum =0.0;
while(s.hasNext()) {
String[] split = s.nextLine().split(", ");
q1sum += Double.parseDouble(split[1]); // add all first grades
q1average++;
q2sum += Double.parseDouble(split[2]); // add all first grades
q2average++;
q3sum += Double.parseDouble(split[3]); // add all first grades
q3average++;
q4sum += Double.parseDouble(split[4]); // add all first grades
q4average++;
proji_sum += Double.parseDouble(split[5]); // add all first grades
proji++;
projii_sum += Double.parseDouble(split[6]); // add all first grades
projii++;
projiii_sum += Double.parseDouble(split[7]); // add all first grades
projiii++;
int maxq1 =0;
if(Double.parseDouble(split[1]) > maxq1)
maxq1 = (int) Double.parseDouble(split[1]);
System.out.println(maxq1);
}
double q1_average = (q1sum/q1average);
double q2_average = (q2sum / q2average);
double q3_average = (q3sum / q3average);
double q4_average = (q4sum / q4average);
double p1_average = (proji_sum/proji);
double p2_average = (projii_sum/projii);
double p3_average = (projiii_sum/projiii);
}
}}catch(Exception e){
e.printStackTrace();
}
}
public void closeFile(){
x.close();
}
}
答案 0 :(得分:2)
与获得纸面平均值的方式相同:添加所有单独的测试分数并除以总数。因此,创建一个临时变量,然后在循环中,将各个测试分数相加,除以循环后的学生总数。
答案 1 :(得分:0)
这不是很好的方法,但它应该工作。因此,您的数组为split[]
,它存储由,
(逗号)拆分的所有数据。因此,如果您注意到,如果您向第一位学生+8 index
添加q
(例如q1
存储在split[1]
中,那么+8,第二{{1} }} Student
将存储在q1
)中,您将获得第二个学生q。所以可以这样做:
split[9]
你可以和其他人一起做q。但正如我所说,这是样板代码。
答案 2 :(得分:0)
基本上你拥有的是一个多维数组。您在多行(“列”)上有多个测试分数(“行”)。你需要将每一行放在某处,以便以后再调用它们。
你可以做这样简单的事情:
double grades = 0.0;
double sum = 0.0;
while(s.hasNext()) {
String[] split = s.nextLine().split(", ");
sum += Double.parseDouble(split[1]); // add all first grades
grades++;
}
System.out.println(sum / grades);
或更复杂的事情:
List<ArrayList<String>> allGrades = new ArrayList<ArrayList<String>>();
while(s.hasNext()) {
allGrades.add(Arrays.asList(s.nextLine().split(", ")));
}
StringBuilder results = new StringBuilder();
int cols = allGrades.get(0).size();
for(int c = 1; ;) { // outer for every test
double sum = 0.0;
double count = 0.0;
double max = 0.0;
for(int r = 0; r < allGrades.size(); r++) { // inner for every person
count++;
double grade = Double.parseDouble(allGrades.get(r).get(c));
sum += grade;
if(grade > max) {
max = grade;
}
}
results.append("[test ");
results.append(c);
results.append(": avg = ");
results.append(sum / count);
results.append(", max = ");
results.append(max);
results.append(']');
if(++c < cols) { // fence post logic...
results.append(", ");
} else {
break;
}
}
System.out.println(results);
或者也许是这样:
double[] avgs = new double[7];
double count = 0.0;
while(s.hasNext()) {
String[] split = s.nextLine().split(", ");
for(int i = 0, k = 1; k < split.length; i++, k++) {
avgs[i] += Double.parseDouble(split[k]);
}
count++;
}
for(int i = 0; i < avgs.length; i++) {
System.out.println("test avg " + (i + 1) + ": " + (avgs[i] / count));
}