Discuss / Java / 交作业:调用Arrays.sort()实现降序

交作业:调用Arrays.sort()实现降序

Topic source

Young-96

#1 Created at ... [Delete] [Delete and Lock User]
int[] ns = {28, 12, 89, 73, 65, 18, 96, 50, 8, 36};
// 排序前:
System.out.println(Arrays.toString(ns));
// TODO:
Arrays.sort(ns);
int[] nsDes = new int[ns.length];
for (int n = ns.length - 1; n >= 0; n--) {
    nsDes[ns.length - n - 1] = ns[n];
}
ns = nsDes;

// 排序后:
System.out.println(Arrays.toString(ns));
if (Arrays.toString(ns).equals("[96, 89, 73, 65, 50, 36, 28, 18, 12, 8]")) {
    System.out.println("测试成功");
} else {
    System.out.println("测试失败");
}

  • 1

Reply