gpt4 book ai didi

android - 如何从 txt 文件中分离和比较字符串

转载 作者:行者123 更新时间:2023-11-30 03:01:06 25 4
gpt4 key购买 nike

我需要你的帮助!

我在一个 txt 文件中混合了任何信息:

我必须根据以下规则分配分数

  • 答对2分;
  • 答错-1分;
  • 未回答问题的分数(带空格)

将总分按照以下等级分类

  • 40~36 =>> A
  • 35~31 =>> B
  • 30~26 =>> C

我的代码是:

    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();
}
}
}

最佳答案

你可以这样做

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;
}

关于android - 如何从 txt 文件中分离和比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22513912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com