数组越界,程序退出了
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3

解决方案 »

  1.   

    在14行的位置数组越界错误
    由于数组a的长度是3,即数组最大为a[2]当i= 2时
    14行的 a[i+1] = a[3],此时已经超过了数组的最大长度
      

  2.   

    第三个for循环条件改成:
    for(int i=0;i<3-j-1;i++){
      

  3.   


    for (int j = 0; j < 3; j++) {
    for (int i = 0; i < 3 - j; i++) {
    if (a[i] > a[i + 1]) {
    int temp = a[i + 1];
    a[i + 1] = a[i];
    a[i] = temp;
    }
    }
    }
    数组越界
      

  4.   


    import java.util.Scanner;public class Main {
    public static void main(String args[]) throws Exception{
    Scanner s= new Scanner(System.in);
    int a[]=new int[3];
    for(int i=0;i<3;i++){
    a[i]=s.nextInt();
    }
    for(int j=0;j<3;j++)
    for(int i=0;i<3-j-1;i++){
    if(a[i]>a[i+1]){
    int temp=a[i+1];
    a[i+1]=a[i];
    a[i]=temp;
    }
    }
                System.out.println("");
       System.out.print(+a[0]+" "+a[1]+" "+a[2]);
      }
    }写好了   谢谢