java数组越界错误,我的这串代码怎么改啊?
packageHomework.Demo;publicclassHomework04{publicstaticvoidmain(String[]args){int[]bo...
package Homework.Demo;public class Homework04 { public static void main(String[] args) { int[] book1 = {123, 156, 187}; int[] book2 = {145, 154, 199, 201}; int[] book = new int[7]; for (int i = 0; i <7 ; i++) { if (i<=2) { book[i] = book1[i]; }else { book[i]=book2[i-3]; } } int temp=0; for (int c = 0; c <7 ; c++) { if (book[c]<book[c++]) { continue; }else{ book[c]=temp; book[c++]=temp; temp=book[c]; } System.out.print("房号"+book[c]); System.out.println("最中间的房号"+book[(book.length-1)/2]); } }}
展开
2019-10-07
展开全部
你这个下标越界是因为你最后一个循环里面有c++但是你循环最后一个角标是6,++成7所以越界,其次book[c]=temp; book[c++]=temp; temp=book[c];你这三条转换的不觉得有点奇怪吗 book[c]<book[c++]还有if里的这个判断 后面的c++是先++了在用角标去拿值了还是先拿值了再去运行++
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询