Java中的冒泡排序问题,已经写好了方法,好像是调用时出现了问题?我也不太清楚,求大神帮忙!
我是菜鸟一个,求大神帮忙看看是怎么回事,系统报错说是数组定义和main方法出错,希望能帮忙改正,并作出解释(解释更重要啊!!)packagepaixuchazhao;//...
我是菜鸟一个,求大神帮忙看看是怎么回事,系统报错说是数组定义和main方法出错,希望能帮忙改正,并作出解释(解释更重要啊!!)
package paixuchazhao;
//用于冒泡排序
public class BubblePaiXu {
//Array=new
int temp;
int [] Array=new int[]{12,95,46,83,86,38,76,94,67,79};
public static void main(String[] args){
Array .bubble();
}
void bubble(int [] Array1){
//Array1=Array;
for(int i=0; i<Array1.length-1 ;i++){
for(int j=1; j<Array1.length ;j++)
if(Array[i]>Array[j]){
temp=Array[i];
Array[i]=Array[j];
Array[j]=temp;
}
for(i=0;i<Array.length ;i++){
System.out.print(Array[i]);
}
}
}
}
这个在编译时就出错了,“Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static field Array”,但是把数组Array改为静态static后,又提示错误“Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot invoke bubble() on the array type int[]” 展开
package paixuchazhao;
//用于冒泡排序
public class BubblePaiXu {
//Array=new
int temp;
int [] Array=new int[]{12,95,46,83,86,38,76,94,67,79};
public static void main(String[] args){
Array .bubble();
}
void bubble(int [] Array1){
//Array1=Array;
for(int i=0; i<Array1.length-1 ;i++){
for(int j=1; j<Array1.length ;j++)
if(Array[i]>Array[j]){
temp=Array[i];
Array[i]=Array[j];
Array[j]=temp;
}
for(i=0;i<Array.length ;i++){
System.out.print(Array[i]);
}
}
}
}
这个在编译时就出错了,“Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static field Array”,但是把数组Array改为静态static后,又提示错误“Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot invoke bubble() on the array type int[]” 展开
2个回答
展开全部
if应该判断Array[j]和Array[j+1];
j从0开始,j<array.length-i;
package paixuchazhao;
//用于冒泡排序
public class BubblePaiXu {
//Array=new
int temp;
int [] Array=new int[]{12,95,46,83,86,38,76,94,67,79};
public static void main(String[] args){
bubble(Array);
}
public static void bubble(int [] Array1){
for(int i=0; i<Array1.length-1 ;i++){
for(int j=0; j<Array1.length-i ;j++){
if(Array1[j]>Array1[j+1]){
temp=Array[j];
Array[j]=Array[j+1];
Array[j+1]=temp;
}
}
}
}
for(i=0;i<Array.length ;i++){
System.out.print(Array[i]);
}
}
j从0开始,j<array.length-i;
package paixuchazhao;
//用于冒泡排序
public class BubblePaiXu {
//Array=new
int temp;
int [] Array=new int[]{12,95,46,83,86,38,76,94,67,79};
public static void main(String[] args){
bubble(Array);
}
public static void bubble(int [] Array1){
for(int i=0; i<Array1.length-1 ;i++){
for(int j=0; j<Array1.length-i ;j++){
if(Array1[j]>Array1[j+1]){
temp=Array[j];
Array[j]=Array[j+1];
Array[j+1]=temp;
}
}
}
}
for(i=0;i<Array.length ;i++){
System.out.print(Array[i]);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询