public class sort {
public static void main(String[] args) {
int i =1;
Scanner input = new Scanner(System.in);
System.out.print("Please enter the number of data points: ");
int data = input.nextInt();
double [] userArray = new double[data];
if(data < 0){
System.out.println("The number should be posotive. Exiting.");
} else {System.out.println("Enter the data:"); }
while (i <= data) {
int userInput = input.nextInt();
i++;
}
insertionSort(userArray);
}
static void insertionSort(double[] arr) {
int i, j;
double newValue;
for (i = 1; i < arr.length; i++) {
newValue = arr[i];
j = i;
while (j > 0 && arr[j - 1] > newValue) {
arr[j] = arr[j - 1];
j--;
}
arr[j] = newValue;
}
System.out.println("The sorted data is: " + arr);
}
}
目前输出如下:
请输入数据点数:5 输入数据:
4
2
5
3
1
排序数据为:[D@3d4eac69
数字全部由用户输入,输出结束时“[D @ 3d4eac69”总是在我打印时输出。