我需要你的帮助!
我在txt文件中混合了任何信息:
我必须根据以下规则分配分数
并根据以下等级对总积分进行分类
我的代码是:
private void readRaw() {
tv.append("\n\nDiretório do arquivo processado: res/raw/history_data.txt:\n");
InputStream is = this.getResources()
.openRawResource(R.raw.history_data);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr, 8192);
try {
String test;
while (true) {
test = br.readLine();
// How do I compare the contents of the first line with the contents of the last 20 characters of each line?
if (test == null)
break;
else
tv.append("\n" + " " + test);
}
isr.close();
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
你可以做这样的事情
private void readRaw() {
tv.append("\n\nDiretório do arquivo processado: res/raw/history_data.txt:\n");
InputStream is = this.getResources()
.openRawResource(R.raw.history_data);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr, 8192);
try {
String test, correctAnswer = null;
int lineNo = 0;
ArrayList<String> studentNames = new ArrayList<String>();
ArrayList<String> answers = new ArrayList<String>();
while (true) {
test = br.readLine();
if (test == null)
break;
else
tv.append("\n" + " " + test);
if(lineNo == 0)
{
correctAnswer = test;
}
else
{
String[] contents = test.split(" ", 2);
studentNames.add(contents[0]);
answers.add(contents[1]);
}
lineNo++;
}
for (String string : answers) {
int result = compare(string, correctAnswer);
// get grade according to points and print the information
}
isr.close();
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
使用compare()
功能将学生的答案与实际答案进行比较
private int compare(String string, String correctAnswer) {
int totalPoints = 0;
if(string.length() != correctAnswer.length())
{
return totalPoints;
}
for (int i = 0; i < string.length(); i++) {
if(string.charAt(i) == correctAnswer.charAt(i) && string.charAt(i) != ' ')
{
totalPoints += 2;
}
else if(string.charAt(i) != ' ')
{
totalPoints -= 1;
}
}
return totalPoints;
}