gpt4 book ai didi

java - ArrayIndexOutOfBoundsException 当向后计数到第一个元素然后返回到最后一个元素时

转载 作者:行者123 更新时间:2023-12-01 17:43:21 25 4
gpt4 key购买 nike

我正在使用一个按钮,在android中移动到上一个问题,我实现了一个问题库,就像一个由5个元素组成的数组,当向后迭代到达第一个元素时,我需要返回到最后一个元素,但得到一个错误java .lang.ArrayIndexOutOfBoundsException: 长度=5;索引=5。

public class MainActivity extends AppCompatActivity {                                          
private Button mTrueButton;
private Button mFalseButton;
private ImageButton mNextButton;
private ImageButton mBackButton;
private TextView mQuestionTextView;

private TrueFalse[] mQuestionBank = new TrueFalse[] {
new TrueFalse(R.string.question_oceans, true),
new TrueFalse(R.string.question_mideast, false),
new TrueFalse(R.string.question_africa, false),
new TrueFalse(R.string.question_americas, true),
new TrueFalse(R.string.question_asia, true),
};

private int mCurrentIndex = 0;

private void updateQuestion() {
int question = mQuestionBank[mCurrentIndex].getmQuestion();
mQuestionTextView.setText(question);
}

public void checkAnswer(boolean userPressedTrue){
boolean answerIsTrue = mQuestionBank[mCurrentIndex].ismTrueQuestion();

int messageResId = 0;

if (userPressedTrue == answerIsTrue){
messageResId = R.string.correct_toast;
} else {
messageResId = R.string.incorrect_toast;
}

Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
}

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

mQuestionTextView = (TextView) findViewById(R.id.question_text_view);

mTrueButton = (Button) findViewById(R.id.true_button);

mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
}
});

mFalseButton = findViewById(R.id.false_button);

mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
}
});

mQuestionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});

mNextButton = (ImageButton) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});

mBackButton = (ImageButton) findViewById(R.id.back_button);
mBackButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Here I get an error getting back to the previous question
if(mCurrentIndex == 0){
mCurrentIndex = mCurrentIndex + mQuestionBank.length;
updateQuestion();
} else {
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
updateQuestion();
}
}
});
updateQuestion();
}

}
当我到达第一个元素时,任何人都可以提供一个想法或解决方案,以返回到我的问题库数组中的最后一个元素。

最佳答案

您正在访问索引。所以,更改您的代码:

if(mCurrentIndex == 0){                                                      
mCurrentIndex = mCurrentIndex + mQuestionBank.length;
updateQuestion();
}

至:

if(mCurrentIndex == 0){                                                      
mCurrentIndex = mCurrentIndex + mQuestionBank.length-1;
updateQuestion();
}

关于java - ArrayIndexOutOfBoundsException 当向后计数到第一个元素然后返回到最后一个元素时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58296828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com