int[] g = {2, 6, 7, 9, 1}; int x; for(int b=0; b < g.length-1; b++){ for(int c=1; c<g.length; c++){ if(g[b] >g[c]){ x = g[b]; g[b] = g[c]; g[c] = x; } } } for(int b=0; b<g.length; b++){ System.out.println(g[b]); }代码如上,老是达不到排序的效果,求大神解答。
1 回答
鈊鍟
TA贡献1条经验 获得超0个赞
package com.test;
public class ceshiDemo {
public static void main(String[] args) {
int[] g = { 2, 6, 7, 9, 1 };
int x;
for (int b = 0; b < g.length ; b++) {
for (int c = 1; c < g.length-1-b; c++) {
if (g[b] > g[c+1]) {
x = g[b];
g[b] = g[c];
g[c] = x;
}
}
}
for (int b = 0; b < g.length; b++) {
System.out.println(g[b]);
}
}
}
添加回答
举报
0/150
提交
取消