java – 字符串数组不匹配

前端之家收集整理的这篇文章主要介绍了java – 字符串数组不匹配前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的程序显示一系列问题(逐个问题).在我写完答案后,一条警告信息应告诉我答案是对还是错.
问题是,即使我写了正确的答案,警报消息也会显示错误”消息.
final String questions[] = {"Who's Tom?","Who's Luca?","Who's Flavie?"}
final String answers[] = {"American","Italian","French"}

// display question
answer_question.setOnClickListener(new View.OnClickListener() {
    int CurrentQuestionIndex = 0;

    public void onClick(View v) {
        ask_question.setText(question[(CurrentQuestionIndex++) % (questions.length)]);

        // discuss question versus answer
        EditText answer = (EditText) findViewById(R.id.tvReponseF);

        if (answer.equals(answers[CurrentQuestionIndex])) {
            alertMessageRight();
        } else {
            alertMessageFalse();
        }
    } 
});

解决方法

问题是您正在将EditText对象与String字段进行比较.您必须将String与String进行比较.

这是怎么做的:

String answer = ((EditText) findViewById(R.id.tvReponseF)).getText().toString();

if(answer.equals(answers[CurrentQuestionIndex]))
{
     ...
原文链接:https://www.f2er.com/java/126691.html

猜你在找的Java相关文章