import java.util.Scanner;
import java.util.Arrays;
public class rr {
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(Array.toString(arr));
}
}
程序应该取用户输入的值,用insertionSort方法对它们进行排序,然后打印出来。我认为这些值正在排序,但由于某些原因它们没有被打印出来。
答案 0 :(得分:0)
sysout
似乎有拼写错误,我们需要使用System.out.println(Arrays.toString(arr));
来打印数组的内容。