完成之后结束数组

时间:2018-02-02 07:56:29

标签: java android arrays

我正在制作一个有很多问题的测验应用程序。我遇到的错误是每当我完成所有测验问题时,我得到这个: $("#taskname").change(function (){ var idc = $("#taskname").prop('selectedIndex'); var tasktypes = jsonData[idc].task; console.log(tasktypes); // include and see what is coming over here in console compos = ""; for (i = 0; i < tasktypes.length; i++) { console.log(tasktypes[i]); // include and see what is coming over here in console compos += "<option value='" + tasktypes[i].componentname + "'>" + tasktypes[i].componentname + "</option>"; }; $('#components').html(compos); }); }); 我知道我的数组已超出范围,但现在如何在完成数组中的所有项目后结束它?

我的代码如下:

java.lang.ArrayIndexOutOfBoundsException:

我正在使用另一个java文件来存储我所有的问题,选择和正确的答案,名为

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

    mScoreView = (TextView)findViewById(R.id.score);
    mQuestionView = (TextView)findViewById(R.id.question);
    mButtonChoice1 = (Button)findViewById(R.id.choice1);
    mButtonChoice2 = (Button)findViewById(R.id.choice2);
    mButtonChoice3 = (Button)findViewById(R.id.choice3);
    mImageView=(ImageButton) findViewById(R.id.imageButton);

    updateQuestion();


    //Start of Button Listener for Button1
    mButtonChoice1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            //My logic for Button goes in here
            counter = counter +1;
            mImageView.setImageResource(imageID[counter]);
            if (mButtonChoice1.getText() == mAnswer){
                mScore = mScore + 1;
                updateScore(mScore);
                updateQuestion();

                //This line of code is optiona
                Toast.makeText(VehicleActivity.this, "correct", Toast.LENGTH_SHORT).show();


            }else {
                Toast.makeText(VehicleActivity.this, "wrong", Toast.LENGTH_SHORT).show();
                updateQuestion();
            }
        }
    });

    //End of Button Listener for Button1

    //Start of Button Listener for Button2
    mButtonChoice2.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            //My logic for Button goes in here
            counter = counter +1;
            mImageView.setImageResource(imageID[counter]);
            if (mButtonChoice2.getText() == mAnswer){
                mScore = mScore + 1;
                updateScore(mScore);
                updateQuestion();
                //This line of code is optiona
                Toast.makeText(VehicleActivity.this, "correct", Toast.LENGTH_SHORT).show();

            }else {
                Toast.makeText(VehicleActivity.this, "wrong", Toast.LENGTH_SHORT).show();
                updateQuestion();
            }
        }
    });

    //End of Button Listener for Button2


    //Start of Button Listener for Button3
    mButtonChoice3.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            //My logic for Button goes in here
            counter = counter +1;
            mImageView.setImageResource(imageID[counter]);

            if (mButtonChoice3.getText() == mAnswer){
                mScore = mScore + 1;
                updateScore(mScore);
                updateQuestion();
                //This line of code is optiona
                Toast.makeText(VehicleActivity.this, "correct", Toast.LENGTH_SHORT).show();

            }else {
                Toast.makeText(VehicleActivity.this, "wrong", Toast.LENGTH_SHORT).show();
                updateQuestion();
            }
        }
    });

    //End of Button Listener for Button3





}

private void updateQuestion(){

    mQuestionView.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
    mButtonChoice1.setText(mQuestionLibrary.getChoice1(mQuestionNumber));
    mButtonChoice2.setText(mQuestionLibrary.getChoice2(mQuestionNumber));
    mButtonChoice3.setText(mQuestionLibrary.getChoice3(mQuestionNumber));

    mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
    mQuestionNumber++;
}


private void updateScore(int point) {
    mScoreView.setText("" + mScore);
}

所以我想知道的是如何结束这个数组循环并向用户显示他们的总分。

1 个答案:

答案 0 :(得分:2)

只需在updateQuestion()函数中添加一个if语句,该函数检查myQuestionNumber var以查看它是否超出了应该回答的问题数量,并在何时执行,将重定向执行到某处它清除按钮并进入下一阶段。

这样的东西
if (myQuestionNumber >= MAX_QUESTION_NUM) {
    clearButtons();
    showResults();
} else {
    myQuestionNumber++;
}

此外,您不需要同时使用counter变量和myQuestionNumber变量。它们用于相同的目的,并存储相同(或逐个)的值。在引用数组时只需使用myQuestionNumber。