【问题标题】:String arrays does not match字符串数组不匹配
【发布时间】:2016-07-08 19:00:39
【问题描述】:

我的程序会显示一系列问题(一个一个问题)。在我写完答案后,一条警告消息应该告诉我我的答案是对还是错。 问题是,即使我写了正确的答案,警报消息也会显示“错误”消息。

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

【问题讨论】:

  • 发布您的 answers 数组。

标签: java android arrays


【解决方案1】:

问题是您将EditText 对象与String 字段进行比较。您必须将StringString 进行比较。

这是怎么做的:

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

if(answer.equals(answers[CurrentQuestionIndex]))
{
     ...

【讨论】:

  • 这还不是全部,这只是一个小问题。他在错误的地方增加了索引计数器,检查我的答案,我也提到了。
【解决方案2】:

问题是你在实际检查答案是否正确之前增加了索引:

ask_question.setText(question[(CurrentQuestionIndex++) % (questions.length)]);

所以如果问题索引为 0,你会得到一个问题 0,那么问题索引将变为 1,因为 ++ 运算符,你将阅读答案 1 而不是 0。我希望你明白这一点。您需要做的是从此处删除++ 并将其放置在此处:

if(answer.getText().toString().equals(answers[(CurrentQuestionIndex++) % (questions.length)]))

因为此时,您将阅读正确答案并继续下一个索引。

编辑

这是你最大的问题。您还需要比较其他人已经注意到的answer.getText().toString()

【讨论】:

    【解决方案3】:

    您需要从EditText 对象中获取文本。您正在将 EditText 对象与 String 进行比较。

    因此,您的完整代码应如下所示:

    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 editText = (EditText) findViewById(R.id.tvReponseF);
            if(editText.getText().toString().equals(answers[CurrentQuestionIndex]))  
            {
                alertMessageRight();
            } else {
                alertMessageFalse();
            }
        } 
     });
    

    【讨论】:

      【解决方案4】:

      试试这个:

      EditText et;//initialize first!!
      et.getText().toString().equals(....);
      

      【讨论】:

        【解决方案5】:
         EditText answer = (EditText) findViewById(R.id.tvReponseF);
        
         if(answer.getText().toString().toLowerCase().equals(answers[CurrentQuestionIndex].toLowerCase()))  
        {
            alertMessageRight();
        } else {
            alertMessageFalse();
        }
        
         } 
        

        我添加了 toLowerCase() 只是让问题不区分大小写。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-06-21
          • 1970-01-01
          • 2019-04-08
          • 2019-07-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多