如何检查项目是否在数组中重复?

时间:2018-08-10 02:10:29

标签: java android android-studio math

我创建了一个带有一些简单问题的数学问答游戏,问题是问题一遍又一遍地重复,是否有可能检查一个问题是否重复,是否重复一次然后问另一个问题,然后结束回答所有问题后进行游戏。我不希望重复任何问题。如何实现?请帮助。这是我的代码。

public class MathsEasy extends Activity {

    Button answer1,answer2,answer3,answer4;
    TextView score,question;
    private MathsEasyQuestions mQuestions  = new MathsEasyQuestions();
    private String mAnswer;
    private int mScore = 0;
    private int mQuestionsLength = mQuestions.mQuestions.length;
    Random r;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.maths_easy);


        r = new Random();


        answer1 = (Button)findViewById(R.id.answer1);
        answer2 = (Button)findViewById(R.id.answer2);
        answer3 = (Button)findViewById(R.id.answer3);
        answer4 = (Button)findViewById(R.id.answer4);


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


        score.setText("Score: " + mScore);
        updateQuestion(r.nextInt(mQuestionsLength));


        answer1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (answer1.getText() == mAnswer){
                    mScore++;
                    score.setText("Score: " + mScore);
                    updateQuestion(r.nextInt(mQuestionsLength));
                    updateQuestion(r.nextInt(mQuestionsLength));
                }  else {

                    gameOver();
                }

            }
        });





        answer2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if (answer2.getText() == mAnswer){
                    mScore++;
                    score.setText("Score: " + mScore);
                    updateQuestion(r.nextInt(mQuestionsLength));
                    updateQuestion(r.nextInt(mQuestionsLength));
                }  else {

                    gameOver();
                }

            }
        });



        answer3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if (answer3.getText() == mAnswer){
                    mScore++;
                    score.setText("Score: " + mScore);
                    updateQuestion(r.nextInt(mQuestionsLength));
                    updateQuestion(r.nextInt(mQuestionsLength));
                }  else {

                    gameOver();
                }

            }
        });




        answer4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if (answer4.getText() == mAnswer){
                    mScore++;
                    score.setText("Score: " + mScore);
                    updateQuestion(r.nextInt(mQuestionsLength));
                    updateQuestion(r.nextInt(mQuestionsLength));
                }  else {

                    gameOver();
                }

            }
        });


    }













    private void updateQuestion(int num){
        question.setText(mQuestions.getQuestion(num));
        answer1.setText(mQuestions.getChoice1(num));
        answer2.setText(mQuestions.getChoice2(num));
        answer3.setText(mQuestions.getChoice3(num));
        answer4.setText(mQuestions.getChoice4(num));


        mAnswer = mQuestions.getCorrectAnswer(num);
    }




    private void gameOver(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MathsEasy.this);
        alertDialogBuilder
                .setMessage("Game Over! Your Score Is " + mScore + " points")
                .setCancelable(false)
                .setPositiveButton("New Game", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        startActivity(new Intent(getApplicationContext(),MathsEasy.class));


                    }
                })


                .setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        startActivity(new Intent(getApplicationContext(),Mathematics.class));

                    }
                });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();


    }


    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this,Mathematics.class);
        startActivity(intent);
    }
}

public class MathsEasyQuestions {

    public String mQuestions[]={

            "2+2 = ?",
            "3+2 = ?",
            "4+2 = ?",
            "9 x 2 = ?",
            "56-22 = ?",
            "100 ÷ 5 = ?",
            "I have 300 stickes I gave 199 to my friend. how many do  I have now ?",
            "1+2+3+4+5+6+7+8+9 = ?",
            "I am 5 years older than my brother.My brother is 14 years old.How old am I?",
            "64 turtles. A quarter of them swim away. How many are left?",
            "7 birds are on a fence. 3 fly away. How many are left?",
            "What is 10 multiplied by 10?",
            "Four times five is?"
    };


    public String mChoices[][] = {

            {"1","6","4","9"},
            {"3","8","2","5"},
            {"6","2","7","1"},
            {"12","18","16","15"},
            {"27","42","34","39"},
            {"20","10","50","25"},
            {"203","114","122","101"},
            {"29","45","35","56"},
            {"18","20","19","22"},
            {"32","34","40","48"},
            {"4","10","5","2"},
            {"110","100","10","90"},
            {"20","25","15","24"},







    };


    public String mCorrectAnswer[]={"4","5","6","18","34","20","101","45","19","48","4","100","20"};


    public String getQuestion(int a){
        String question = mQuestions[a];
        return question;
    }

    public String getChoice1(int a){
            String choice  = mChoices[a][0];
            return choice;
    }


    public String getChoice2(int a){
        String choice  = mChoices[a][1];
        return choice;
    }


    public String getChoice3(int a){
        String choice  = mChoices[a][2];
        return choice;
    }



    public String getChoice4(int a){
        String choice  = mChoices[a][3];
        return choice;
    }


    public String getCorrectAnswer(int a){
        String answer = mCorrectAnswer[a];
        return answer;
    }




}

0 个答案:

没有答案