Java Android Studio Open Answer Quiz执行错误



我使用了Youtube教程:https://www.youtube.com/watch?v=AkUfACbi6BE使用Java在Android Studio中创建开放式答案测验。我完全遵循了教程,但当我在设备上运行代码时,即使我答错了问题,它也会说我赢了游戏。我不知道为什么它不能正确地验证答案。任何关于如何解决这个问题的提示都将非常棒!该应用程序的代码如下,因为视频没有提供源代码,所以认为它会很有用。

数据库类:

package com.tests.shortanswerquiz;
public class Database {
public static String[] questions = {
"Which is the first planet in the Solar system?",
"Which is the second planet in the Solar system?",
"Which is the third planet in the Solar system?",
"Which is the fourth planet in the Solar system?",
"Which is the fifth planet in the Solar system?",
"Which is the sixth planet in the Solar system?",
"Which is the seventh planet in the Solar system?",
"Which is the eighth planet in the Solar system?",
"Which is a dwarf planet in the Solar system?",
};
public static String[] answers = {
"mercury",
"venus",
"earth",
"mars",
"jupiter",
"saturn",
"uranus",
"neptune",
"pluto",
};
}

物品类别:

package com.tests.shortanswerquiz;
public class Item {
private String question, answer;
public Item(String question, String answer) {
this.question = question;
this.answer = answer;

}
public String getQuestion() {
return question;
}
public String getAnswer() {
return answer;
}

}

主要活动类别:

package com.tests.shortanswerquiz;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Button b_continue;
TextView tv_question;
EditText et_answer;
List<Item> questions;
int curQuestion = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b_continue = (Button) findViewById(R.id.b_continue);
tv_question = (TextView) findViewById(R.id.tv_question);
et_answer = (EditText) findViewById(R.id.et_answer);
b_continue.setVisibility(View.INVISIBLE);
questions = new ArrayList<>();
for (int i = 0; i < Database.questions.length; i++) {
questions.add(new Item(Database.questions[i], Database.answers[i]));
}

Collections.shuffle(questions);
tv_question.setText(questions.get(curQuestion).getQuestion());
et_answer.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(et_answer.getText().toString().equalsIgnoreCase(questions.get(curQuestion).getAnswer())) {
b_continue.setVisibility(View.VISIBLE);
} else {
b_continue.setVisibility(View.VISIBLE);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
b_continue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(curQuestion < (Database.questions.length - 1)) {
curQuestion++;
tv_question.setText(questions.get(curQuestion).getQuestion());
b_continue.setVisibility(View.INVISIBLE);
et_answer.setText("");
} else {
Toast.makeText(MainActivity.this, "You won the game!", Toast.LENGTH_SHORT).show();
finish();
}
}
});
}
}

XML文件代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/b_continue"
android:layout_width="match_parent"
android:layout_height="61dp"
android:text="CONTINUE" />
<TextView
android:id="@+id/tv_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/b_continue"
android:layout_centerHorizontal="true"
android:gravity="center"
android:padding="10dp"
android:text=" " />
<EditText
android:id="@+id/et_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_question"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="answer"
android:inputType="textPersonName"
android:text="Name" />
</RelativeLayout>

代码失败的原因是这段代码。

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(et_answer.getText().toString().equalsIgnoreCase(questions.get(curQuestion).getAnswer())) {
b_continue.setVisibility(View.VISIBLE);
} else {
b_continue.setVisibility(View.VISIBLE);//set this to View.INVISIBLE instead
}
}

代码的编写方式是,在给出正确答案之前,您无法转到代码的下一部分。当你将其设置为View.VISIBLE时,它会让你在没有正确回答的情况下进入下一个问题。

最新更新