//Test sort method
import java.util.*;
import java.math.*;
public class TestSortMethod {
int i,j,temp;
class TestMethod {
TestMethod(int n){
Scanner nb=new Scanner(System.in);
Integer[] num=new Integer[n];
for( i=0;i<n;i++){
System.out.print("number["+(i+1)+"]=:");
num[i]=nb.nextInt();
}
for( i=0;i<n-1;i++)
for( j=0;j<n-i;j++){
if(num[j]>num[j+1]){
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
for(i=0;i<n;i++){
System.out.println("The sorted numbers are:");
System.out.print(num[i]);
}
}

public static void main(String[] args) {
int num;
Scanner nb=new Scanner(System.in);
System.out.println("Input the value of the numbers' amount:");
num=nb.nextInt();
TestMethod tm=new TestMethod(num);
}
}
*************************************************************************************
TestSortMethod.java:33: non-static variable this cannot be referenced from a static context
TestMethod tm=new TestMethod(num);
                              ^
1 error

解决方案 »

  1.   

    TestSortMethod tsm = new TestSortMethod();
            TestMethod tm = tsm.new TestMethod(num);
      

  2.   

    Input the value of the numbers' amount:
    number[1]=:number[2]=:number[3]=:number[4]=:number[5]=:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at TestSortMethod$TestMethod.<init>(TestSortMethod.java:16)
    at TestSortMethod.main(TestSortMethod.java:34)
    ***************************************************************
    运行出现异常?
      

  3.   

    数组下标越界了。for (j = 0; j < n - i - 1; j++)或许应该是这样子。