读取java中包含布尔值的两个txt文件,并创建2d数组
我是java新手,我使用的代码是在导师的帮助下创建的。我似乎不明白为什么我的输出不正确。例如,正确回答的答案数量与txt文件不匹配 设计并实现一个应用程序,从responses.txt将2000个成绩读入2d数组,该数组表示100名学生20个真假问题的测试结果。数组的每一行代表100名学生中的1名学生的答案。每列代表每个学生对考试中特定问题的答案。数组的每个单元格都包含布尔值“true”或“false”。从另一个文件key.txt中读入的值将存储在第二个1d数组中,该数组包含20个布尔值,代表对20个问题的正确答案。你的程序应该以图表形式计算并打印出每个学生正确答案的#,以图表形式正确回答20个问题的学生的#,平均测验分数和分数的标准差 responses.txt:(我在这篇文章中的字符有限,所以我只发布了2000个值中的60个): key.txt:读取java中包含布尔值的两个txt文件,并创建2d数组,java,arrays,Java,Arrays,我是java新手,我使用的代码是在导师的帮助下创建的。我似乎不明白为什么我的输出不正确。例如,正确回答的答案数量与txt文件不匹配 设计并实现一个应用程序,从responses.txt将2000个成绩读入2d数组,该数组表示100名学生20个真假问题的测试结果。数组的每一行代表100名学生中的1名学生的答案。每列代表每个学生对考试中特定问题的答案。数组的每个单元格都包含布尔值“true”或“false”。从另一个文件key.txt中读入的值将存储在第二个1d数组中,该数组包含20个布尔值,代表对
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;
}
}
我的代码:
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;
}
}
查看Responses.txt文件,似乎有2000行?或者有100行20个独立的值(这就是实现的方式) 对于2000行,文件每行有1个输入。现在的逻辑是,如果有100行20个输入(用空格分隔),则设置逻辑 另外,将2d数组更改为“对象”类型 这是伪代码:
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
}
int行=0;
字符串temp=“”;
int=0;
//将在文件完成时终止
而((temp=br.readLine())!=null)
{
如果(第20行)
{
//给学生添加答案
//将1添加到第行
}否则{
//向学生添加1
//给学生添加答案
}
responses.txt文件确实有2000行。我的输出应该有100行,其中有20个独立的值。我是否应该在if/break中更改某些内容?是的,当然,您甚至在代码中有注释,说明该逻辑实际上在做什么。您已经在处理while循环中的终止条件。我在更改时出错ge to:if(行>=2000)运行:线程“main”java.lang.ArrayIndexOutOfBoundsException中异常:100级。main(Grades.java:38)C:\Users\kubert\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53:java返回:1次生成失败(总时间:0秒)你还需要去掉下面的for循环。基本上,你的行计数器应该担心会碰到该文件中的第20个答案。一旦碰到了,你就转到下一个学生,重置行计数器。另外,我会将2d数组数据类型更改为Object。这样你就可以在第一个数组中存储一个整数(即哪个学生)和第二个数组中的布尔值。[1][{true,false,…,第20个布尔值}][2][{true,false,…,第20个布尔值}].[20][{true,false,…,第20个布尔值}]这似乎没有改变我的任何输出…最初,我的代码没有读取到每一个值都在下一行,而不是用空格分隔。我的代码是否正确地将每一行与中间的空格连接在一起?
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
}