如何在测验应用程序中从随机排列的数组中检查答案

时间:2018-07-06 11:34:08

标签: android shuffle

我已经关注了该网站上的另一则帖子,内容是关于如何对测验应用程序的问题和答案进行混洗,但是我需要以下方面的帮助:(a)在textview中显示问题,以及(b)在我不检查时从edittext检查答案知道如何从改组后的数组中调用它们。

主要活动文件-

   import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;



public class MainActivity extends AppCompatActivity {




    Button submit_btn, next_btn;

    TextView scoremm6, question;

    EditText answer;

    String manswer;

    public TextView getScoreT2() {
        return scoremm6;
    }

    private int mScore = 0;
    private boolean done;
    private int QuestionNo = 0;

    private int mQuestionNumber = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.answer).setBackgroundResource(R.drawable.rounded_cornerquestion);
        findViewById(R.id.submit_btn).setEnabled(true);



        final Button enter_btn = (Button) findViewById(R.id.submit_btn);
        answer = (EditText) findViewById(R.id.answer);

        scoremm6 = (TextView) findViewById(R.id.scoremm6);
        question = (TextView) findViewById(R.id.question);

        scoremm6.setText("Score: " + mScore);

        List<Questions> questions = new ArrayList<Questions>();

        questions.add(
                new Questions(
                        "3 x 5",
                        "15"
                )
        );
        questions.add(
                new Questions(
                        "4 x 5",
                        "20"
                )
        );
        questions.add(
                new Questions(
                        "5 x 5",
                        "25"
                )
        );
        questions.add(
                new Questions(
                        "6 x 5",
                        "30"
                )
        );
        questions.add(
                new Questions(
                        "7 x 5",
                        "35"
                )
        );
        questions.add(
                new Questions(
                        "8 x 5",
                        "40"
                )
        );
        questions.add(
                new Questions(
                        "9 x 5",
                        "45"
                )
        );
        questions.add(
                new Questions(
                        "10 x 5",
                        "50"
                )
        );
        questions.add(
                new Questions(
                        "11 x 5",
                        "55"
                )
        );
        questions.add(
                new Questions(
                        "12 x 5",
                        "60"
                )
        );
        questions.add(
                new Questions(
                        "2 x 4",
                        "8"
                )
        );
        questions.add(
                new Questions(
                        "3 x 4",
                        "12"
                )
        );
        questions.add(
                new Questions(
                        "4 x 4",
                        "16"
                )
        );
        questions.add(
                new Questions(
                        "5 x 4",
                        "15"
                )
        );
        Collections.shuffle(questions);

///这是我需要帮助的地方,可以从经过改组的数组中检索正确的答案

        Button Submit = (Button) findViewById(R.id.submit_btn);
        Submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (done == false) {
                    String answer = ((EditText) findViewById(R.id.answer)).getText().toString();
                    String[] answers1 = getString(new ArrayList<Questions>manswer;
                    String correctanswer = answers1[QuestionNo];
                    //gets the answer and correct answer from the edit text and strings.xml respectively
                    correctanswer = correctanswer;
                    answer = answer;


                    if (answer.equals(correctanswer)) {
                        mScore++;
                        findViewById(R.id.answer).setBackgroundResource(R.drawable.rounded_cornerbuttonscorrect);
                        scoremm6.setText("Score: " + mScore);
                        enter_btn.setEnabled(false);
                        Toast.makeText(getApplicationContext(), "Correct.",
                                Toast.LENGTH_SHORT).show();


                    } else {
                        findViewById(R.id.answer).setBackgroundResource(R.drawable.rounded_cornerbuttonsincorrect);
                        enter_btn.setEnabled(false);
                    }

                }

            }


        });

///我在这里也需要帮助以解决另一个问题

        Button Next2 = (Button) findViewById(R.id.next_btn);
        Next2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                String[] mquestions =  getString(new ArrayList<Questions>);
                if (QuestionNo < (mquestions.length -1)) {
                    QuestionNo = QuestionNo + 1;
                    findViewById(R.id.answer).setBackgroundResource(R.drawable.rounded_cornerquestion);
                    enter_btn.setEnabled(true);
                    answer.getText().clear();
                    TextView t = (TextView) findViewById(R.id.question);
                    t.setText(mquestions[QuestionNo]);


                }


                {

                    if (QuestionNo == (questions.length)) {
                        enter_btn.setEnabled(false);
                        Toast.makeText(getApplicationContext(), "Test Completed",
                                Toast.LENGTH_LONG).show();

                    }

                }

            }


        });
    }
} 

1 个答案:

答案 0 :(得分:0)

您最终可以做到

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    private HashMap<String, Question> remainingQuestions;
    private HashMap<String, Question> answeredQuestions;
    private Question currentQuestion;
    private int score = 0;

    private TextView scoreField;
    private TextView statement;
    private EditText answer;
    private Button checkAnswer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scoreField = findViewById(R.id.score);
        statement = findViewById(R.id.statement);
        answer = findViewById(R.id.answer);
        checkAnswer = findViewById(R.id.check_answer);

        checkAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                final String givenAnswer = answer.getText().toString();
                if(givenAnswer.equals(currentQuestion.getAnswer())) {
                    remainingQuestions.remove(currentQuestion.getStatement());
                    answeredQuestions.put(currentQuestion.getStatement(), currentQuestion);

                    score++;
                    scoreField.setText(String.valueOf(score));

                    Toast.makeText(v.getContext(), "Good answer", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(v.getContext(), "Wrong answer", Toast.LENGTH_LONG).show();
                }

                if(remainingQuestions.size() != 0) {
                    askNewQuestion();
                }
            }
        });

        remainingQuestions = new HashMap<>();
        answeredQuestions = new HashMap<>();

        initQuestions();
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();

        askNewQuestion();
    }

    private void initQuestions() {
        Log.d(TAG, "initQuestions");

        remainingQuestions.put("2 x 3 ?", new Question("2 x 3 ?", "6"));
        remainingQuestions.put("2 x 4 ?", new Question("2 x 4 ?", "8"));
        remainingQuestions.put("2 x 5 ?", new Question("2 x 5 ?", "10"));
        remainingQuestions.put("2 x 6 ?", new Question("2 x 6 ?", "12"));
        remainingQuestions.put("2 x 7 ?", new Question("2 x 7 ?", "14"));
    }

    private void askNewQuestion() {
        Log.d(TAG, "askNewQuestion");

        final Random random = new Random();
        currentQuestion = (Question) remainingQuestions.values().toArray()[random.nextInt(remainingQuestions.size())];

        statement.setText(currentQuestion.getStatement());
        answer.setText("");
    }
}