import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = input.nextInt(); } Arrays.sort(nums); for (int i = 0; i < n; i++) { System.out.print(nums[i] + " "); } } }修改这段代码,使其每个元素之间用一个空格隔开,但是最后一个元素后面没有空格
时间: 2023-05-28 09:06:27 浏览: 78
java笔试算法题及答案
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = input.nextInt();
}
Arrays.sort(nums);
for (int i = 0; i < n; i++) {
System.out.print(nums[i]);
if (i != n - 1) {
System.out.print(" ");
}
}
}
}
阅读全文