java - 错误-> java.lang.ArrayIndexOutOfBoundsException:8
问题描述
我创建了这个多项选择程序,一切都很好,正确的答案正在打印出来,但我不断得到:
线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 8 在 MultipleChoices.main(MultipleChoices.java:21)
有人可以告诉我我需要做什么来解决这个错误吗?
for(int i = 0; i < student[i].length; i++){
int rightAns = 0;
for(int j = 0; j < student[i].length; j++){
if(student[i][j].equalsIgnoreCase(key[j])){
rightAns++;
}
}
解决方案
您的第一个 for 循环使用了错误的值。你应该使用 student.length 而不是 student[i]。
for(int i = 0; i < student.length; i++){
int rightAns = 0;
for(int j = 0; j < student[i].length; j++){
if(student[i][j].equalsIgnoreCase(key[j])){
rightAns++;
}
}
System.out.print("Student's " + i + "#correct answer: " + rightAns + "\n");
}
}
推荐阅读
- python-3.x - 谷歌 AI 平台上的 JupyterLab 笔记本在进行预测时速度超慢
- rust - 如何在迭代对 Result 的引用时使用问号运算符?
- javascript - 验证二叉搜索树的 JavaScript 类解决方案
- unit-testing - Vue 单元测试:如何使用 props、vuex store、watchers、getter 等测试复杂组件
- python - 在数据框中按条件对值进行排序
- java - 使用 Junit 5 在 Java 中进行模拟
- html - 我可以在 CSS 中创建一个底部弯曲的 div 吗?
- javascript - 在 React 中,我从 mongo 调用两个列表到页面,我想单击一个来过滤其他显示的内容
- c# - FileInfo[] files = dir.GetFiles(fileSearch, SearchOption.TopDirectoryOnly) 不返回目录中存在的文件
- c# - 矩形不会出现在表单框中