我正在尝试将DB中的项目与EditText(用户输入)的值进行比较。答案可以有多个答案,用","分隔。我先把它们放进字符串中,然后把它们与答案进行比较。LevensteinDistance检查答案是好是坏(http://en.wikipedia.org/wiki/Levenshtein_distance#Computing_Levenshtein_distance)。
userAnswer = etUserAnswer.getText().toString().toLowerCase();
String[] answers = qAnswer.split(",");
for (String answer : answers) {
if (answer.equals(userAnswer)) {
Toast.makeText(getApplicationContext(), ("Answer Correct"),
Toast.LENGTH_SHORT).show();
tvMessage.setText("You smartass!");
} else {
Toast.makeText(getApplicationContext(), ("Wrong"),
Toast.LENGTH_SHORT).show();
points = points - 4;
String answerGood = answer.toLowerCase();
LevenshteinDistance lDistance = new LevenshteinDistance();
int comparisonCheck = lDistance.computeLevenshteinDistance(
userAnswer, answerGood);
if (comparisonCheck == 1) {
tvMessage.setText("Almost there, but not quite yet!");
} else if (comparisonCheck > 1) {
tvMessage.setText("Are you serious, totally wrong?!");
}
}
}
假设我在DB中有一个问题的答案如下:树、测试、无线电
我有两个问题:
1。当我键入"radi"时,它会给我"几乎到了…"这很好。如果我输入"tes",它也应该给我这个,但它会给我"你是认真的吗,…"线我想它一直在和上一个比较。
2。每次我输入不正确的内容时,我得到的是-12,而不是-4。我想这是因为我有三个答案,它循环了三次。。但我不知道我怎么能让它只计数一次。。
路上有人能帮我吗?谢谢
假设你不需要知道给出最小Levenstein距离的单词,你可以修改你的循环,只找到最小距离;
userAnswer = etUserAnswer.getText().toString().toLowerCase();
String[] answers = qAnswer.split(",");
LevenshteinDistance lDistance = new LevenshteinDistance();
int minDistance = lDistance.computeLevenshteinDistance(
userAnswer, answers[0].toLowerCase());
for (int i = 1; i < answers.length; ++i) {
minDistance = Math.min(minDistance, lDistance.computeLevenshteinDistance(
userAnswer, answers[i].toLowerCase()));
}
if (minDistance == 0) {
// Correct answer...
} else {
// Wrong answer...
points -= 4;
// etc etc...
}