我是java新手,我使用的代码是在导师的帮助下创建的。我似乎无法弄清楚为什么我的输出不正确。例如,正确回答的答案数与txt文件不匹配。
设计并实现一个应用程序,该应用程序将来自respond.txt的2000个等级读入2d数组,该应用程序代表对100名学生的20个真假问题的测试结果。数组的每一行代表100名学生中的1名的答案。每列代表每个学生对测试中特定问题的答案。数组的每个单元格包含布尔值“true”或“false”。从另一个文件key.txt中读取值,以存储在20个布尔值的第二个1d数组中,这些布尔值表示20个问题的正确答案。你的程序应该以图表的形式计算和打印每个学生的正确答案的数量,以图表的形式得到20个问题中的每一个的学生数量,平均测验等级&等级的标准差。
responses.txt :(我在这篇文章中的字符数量有限,所以我只发布了2000个值中的60个):
true
true
true
true
true
true
true
true
true
true
false
true
false
true
false
true
false
true
false
true
true
true
true
true
true
true
true
true
true
true
false
true
false
true
false
true
false
true
false
true
true
true
true
true
true
true
true
true
true
true
false
true
false
true
false
true
false
true
false
true
key.txt:
true
true
true
true
true
true
true
true
true
true
false
true
false
true
false
true
false
true
false
true
我的代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class Grades {
public static void main(String[] args) {
// Student Data
boolean[][] studentAnswers = new boolean[100][20];
String responses = "C:\\Users\\kubert\\Documents\\NetBeansProjects\\TwoDArray\\src\\twodarray\\responses.txt";
// Answers data
boolean answers[] = new boolean[100];
String answersFile = "C:\\Users\\kubert\\Documents\\NetBeansProjects\\TwoDArray\\src\\twodarray\\key.txt";
int[] allCorrect = new int[100]; // stores indexes of students who scored all correct
int[] score = new int[100]; // scores of students
try {
BufferedReader br = new BufferedReader(new FileReader(responses));
int row = 0;
String temp = "";
while ((temp = br.readLine()) != null)
{
if (row >= 100)
{
break; // break loop if file contains more than 100 student entries
}
String[] tokens = temp.split(" "); // Assumming each line in txt
// file is one student and
// results are separated by
// space for each question.
for (int i = 0; (i < tokens.length && i < 20); i++)
{
studentAnswers[row][i] = Boolean.valueOf(tokens[i]);
}
row++;
}
}
catch (IOException e)
{
System.out.println("ERROR : " + e);
}
// Reading answers from file
try {
BufferedReader br = new BufferedReader(new FileReader(answersFile));
int index = 0;
String temp = "";
while ((temp = br.readLine()) != null) {
answers[index] = Boolean.valueOf(temp); // Assuming each line in
// answers.txt file is
// answer for each
// question
index++;
}
} catch (IOException e) {
System.out.println("ERROR: " + e);
}
// Prints correct answers of each student
for (int i = 0; i < 100; i++) {
System.out.print("Student " + (i + 1) + " -> ");
int noOfCorrect = 0;
for (int j = 0; j < 20; j++) {
if (studentAnswers[i][j] == answers[j]) {
System.out.print(j + "\t");
noOfCorrect++;
} else {
System.out.print("-" + "\t");
}
}
if (noOfCorrect == 20) {
allCorrect[i] = i;
}
score[i] = noOfCorrect * 5;
System.out.println("\nNo of correct answers : " + noOfCorrect);
System.out.println("Grade Score : " + getGrade(score[i]));
}
// Average Grade Score and Standard Deviation
HashMap<String, List<Integer>> map = new HashMap<>();
map.put("A", new ArrayList<Integer>());
map.put("B", new ArrayList<Integer>());
map.put("C", new ArrayList<Integer>());
map.put("D", new ArrayList<Integer>());
map.put("F", new ArrayList<Integer>());
for (int studentScore : score) {
String grade = getGrade(studentScore);
switch (grade) {
case "A":
map.get("A").add(studentScore);
break;
case "B":
map.get("B").add(studentScore);
break;
case "C":
map.get("C").add(studentScore);
break;
case "D":
map.get("D").add(studentScore);
break;
case "F":
map.get("F").add(studentScore);
break;
}
}
Set<String> keys = new TreeSet(map.keySet());
for (String key : keys) {
System.out.println("Standard deviation " + key + " : " + printSD(map.get(key)));
}
}
/*
* To calculate the standard deviation of those numbers: 1. Work out the
* Mean (the simple average of the numbers) 2. Then for each number:
* subtract the Mean and square the result. 3. Then work out the mean of
* those squared differences. 4. Take the square root of that and we are
* done!
*/
public static double printSD(List<Integer> list) {
double sum = 0;
if (list.size() == 0)
return 0.0;
for (int val : list)
sum += val;
double mean = sum / list.size();
for (int i = 0; i < list.size(); i++) {
sum += Math.pow((list.get(i) - mean), 2);
}
if (sum == 0)
return 0.0;
return Math.sqrt(sum / list.size());
}
public static String getGrade(int score) {
String grade = "";
if (score >= 90)
grade = "A";
else if (score >= 80)
grade = "B";
else if (score >= 70)
grade = "C";
else if (score >= 60)
grade = "D";
else
grade = "F";
return grade;
}
}
我目前得到的输出:
run:
Student 1 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 2 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 3 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 4 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 5 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 6 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 7 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 8 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 9 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 10 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 11 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 12 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 13 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 14 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 15 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 16 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 17 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 18 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 19 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 20 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 21 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 22 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 23 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 24 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 25 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 26 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 27 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 28 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 29 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 30 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 31 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 32 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 33 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 34 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 35 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 36 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 37 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 38 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 39 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 40 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 41 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 42 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 43 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 44 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 45 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 46 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 47 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 48 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 49 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 50 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 51 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 52 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 53 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 54 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 55 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 56 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 57 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 58 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 59 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 60 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 61 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 62 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 63 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 64 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 65 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 66 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 67 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 68 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 69 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 70 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 71 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 72 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 73 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 74 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 75 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 76 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 77 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 78 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 79 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 80 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 81 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 82 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 83 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 84 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 85 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 86 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 87 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 88 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 89 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 90 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 91 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 92 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 93 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 94 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 95 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 96 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 97 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 98 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Student 99 -> - - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 5
Grade Score : F
Student 100 -> 0 - - - - - - - - - 10 - 12 - 14 - 16 - 18 -
No of correct answers : 6
Grade Score : F
Standard deviation A : 0.0
Standard deviation B : 0.0
Standard deviation C : 0.0
Standard deviation D : 0.0
Standard deviation F : 5.782516753110189
BUILD SUCCESSFUL (total time: 1 second)
答案 0 :(得分:0)
查看Responses.txt文件,看来有2000行?或者有100行有20个分隔值(这是你实现的方式)?
if (row >= 100)
{
break; // break loop if file contains more than 100 student entries
}
String[] tokens = temp.split(" "); // Assumming each line in txt
// file is one student and
// results are separated by
// space for each question.
您的文件每行有1行输入2000行。如果有100条线路有20个输入(以空格分隔),则设置现在的逻辑方式。
此外,将您的二维数组更改为&#39;对象&#39;。
的类型这是伪代码:
int row = 0;
String temp = "";
int currStudent = 0;
//will terminate when file is complete
while ((temp = br.readLine()) != null)
{
if (row < 20)
{
//add answer to student
//add 1 to row
} else {
//add 1 to student
//add answer to student
}