坚持比较答案

时间:2014-01-20 13:10:49

标签: android

我正在制作一个测验应用程序。我坚持比较答案。我添加了一个对话框,显示答案是否正确,它是否运作良好,以及吐司。但是当我比较每个单词的错误答案时,我输入I得到toast错误。这里的问题是我用来比较的代码

boolean isCorrectAnswer() {
    String player_answer = "";
    //concat all letters from answer buttons in one string
    for (int i = 0; i < answer_line.length; i++) {
        for (int j = 0; j < answer_line[i].getChildCount(); j++) {

            Button b = (Button) answer_line[i].getChildAt(j);
            player_answer += b.getText().toString();

        }
      //compare player's answer with correct
    if (player_answer.equalsIgnoreCase(correct_answer)) {
        ShowToast(check_toast, "Correct", Gravity.CENTER, new Point(0, -50));

                   return true;
    } else {


        ShowToast(check_toast, "Wrong", Gravity.CENTER, new Point(0, -50));

        return false;
    }


}

***更新了编码

public boolean onTouch(View v, MotionEvent event) {
                    if ((event.getAction() == MotionEvent.ACTION_MOVE) || (event.getAction() == MotionEvent.ACTION_DOWN)) {
                        if (!b.getText().toString().equals("") && letters_in_answer < correct_answer.length()) {
                            letters_in_answer++;
                            System.out.println("letters: " + letters_in_answer);
                            int free_pos = getFirstFreeAnswerButtonPosition();
                            Button a = findButtonByPos(answer_line, free_pos);
                            a.setText(b.getText());
                            hideButton(b);
                            link_list.set(free_pos, letter_pos);
                            Helper.playSound(getApplicationContext(), "click");


                        }

                        //pops during correct answer
                            if (isCorrectAnswer()) {
                                       toUpperCase() + "!", Gravity.CENTER, new Point(0, -50));
                                Helper.playSound(getApplicationContext(), "correct");

                                //if not all images was shown we show next image
                                if (cur_image < images.length - 1) {
                                    final Dialog dialog = new Dialog(context);
                                    dialog.setContentView(R.layout.custom);
                                    dialog.setTitle("Title...");

                                    // set the custom dialog components - text, image and button
                                    TextView text = (TextView) dialog.findViewById(R.id.text);
                                    text.setText("Android custom dialog example!");
                                    ImageView image = (ImageView) dialog.findViewById(R.id.image);
                                    image.setImageResource(R.drawable.ic_launcher);
                                    dialog.show();

                                    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                                    // if button is clicked, close the custom dialog
                                    dialogButton.setOnClickListener(new OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            dialog.dismiss();

                                        }
                                      });

                                    generateNewLevel();
                       } 

                                else {
                                    finish();

                                    Intent endgame = new Intent(GameActivity.this, EndActivity.class);
                                  startActivity(endgame);                                     
                                   finish();
                                }




             }
                        }



                    return false;
                }
            });
        }
    }
}


    }

1 个答案:

答案 0 :(得分:0)

@ user3215214如果用户必须输入所有字母,请在isCorrectAnswer()方法中尝试:

boolean isCorrectAnswer() {
 String player_answer = "";
    //concat all letters from answer buttons in one string
 for (int i = 0; i < answer_line.length; i++) {
    for (int j = 0; j < answer_line[i].getChildCount(); j++) {

        Button b = (Button) answer_line[i].getChildAt(j);
        player_answer += b.getText().toString();

    }
  //compare player's answer with correct
if(player_answer.length()==correct_answer.length()){
    if (player_answer.equalsIgnoreCase(correct_answer)) {
        ShowToast(check_toast, "Correct", Gravity.CENTER, new Point(0, -50));

               return true;
    } else {


        ShowToast(check_toast, "Wrong", Gravity.CENTER, new Point(0, -50));

        return false;
    }
} else return false;


}
相关问题