当前位置: 代码迷 >> Eclipse >> 小白提问,谁帮小弟我看看这题错哪了,多谢
  详细解决方案

小白提问,谁帮小弟我看看这题错哪了,多谢

热度:74   发布时间:2016-04-23 13:45:23.0
小白提问,哪位高手帮我看看这题哪里错了,谢谢
import java.applet.*;import java.util.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;
public class PX {
public static void main(String[] args) {
int px[]={12,9,45,96,5,78,41};
int i=0,j,temp=0,n;
n=px.length;
for(j=0;j<=n-1;j++){
for(i=1;i<=n-1-j;i++){
while(px[i]>px[i+1])
temp=px[i];
px[i]=px[i+1];
px[i+1]=temp;}}
for(i=0;i<=n-1;i++){
System.out.println(px[i]);}
 

}

}
这题是整数序列排序,打印输出。
运行结果:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at Edfdfdf.main(Edfdfdf.java:9)
哪里错了,谁可以帮我看看,谢谢了。555555555

------解决方案--------------------
while 用if 代替

package com.cdm;

public class TestA {
public static void main(String[] args) {
int px[] = { 12, 9, 45, 96, 5, 78, 41 };
int i = 0, j, temp = 0, n;
n = px.length;
for (j = 0; j <= n - 1; j++) {
for (i = 1; i <= n - 1 - j; i++) {
System.out.println(j+ "--"+ i + "---" + (n-1-j)) ;
if(px[i-1] > px[i ]){
temp = px[i-1];
px[i-1] = px[i];
px[i] = temp;
}
}
}
for (i = 0; i <= n - 1; i++) {
System.out.println(px[i]);
}

}

}


5
9
12
41
45
78
96
  相关解决方案