Scanner input = new Scanner(System.in);
int[] num = new int[5];
for (int i = 0; i < num.length; i++) {
System.out.println("请输入第" + (i + 1) + "个值:");
num[i] = input.nextInt();
}
System.out.println("排序后的值为:");
Arrays.sort(num);
/**
* 遍历输出排序后的值
*/
for (int i = 0; i < num.length; i++) {
System.out.println(num[i] + " ");
}
/**
* 定义一个新的数组
*/
int[] xinnum = new int[num.length + 1];
/**
* 从旧的数组中取值
*/
for (int k = 0; k < num.length; k++) {
xinnum[k] = num[k];
}
System.out.println("请输入一个值:");
int zhi = input.nextInt();
/**
* 保存新增数的位置
*/
int index = xinnum.length;
/**
* 确定下标的位置
*/
for (int m = 0; m < xinnum.length; m++) {
if (zhi > xinnum[m]) {
index = m;
break;
}
}
/**
* 将元素后移
*/
for (int t = xinnum.length - 1; t > index; t--) {
xinnum[t] = xinnum[t - 1];
}
// 插入数据
xinnum[index] = zhi;
System.out.println("下标为:" + index);
System.out.println("插入的成绩是:");
// 循环输入目前数组中的数据
for (int i = 0; i < xinnum.length; i++) {
System.out.println(xinnum[i] + " ");
}