![]()
1 package com.workprojects;
2
3 import java.util.Scanner;
4
5 /**
6 * 插入数值
7 * 有一组学员的成绩{99,85,82,63,60}
8 * 按降序排列,再增加一个学员的成绩,插入成绩数列,保持降序
9 * 2019-07-03
10 * @author L
11 *
12 */
13 public class Work070302 {
14 static Scanner sc = new Scanner(System.in);
15 public static void main(String[] args) {
16 int[]scores= new int[6];//定义数组长度
17 scores[0]=99;
18 scores[1]=85;
19 scores[2]=82;
20 scores[3]=63;
21 scores[4]=60;
22 System.out.println("请输入新增成绩:");
23 int newScore = sc.nextInt();
24 int index = -1;//定义指代下标
25 for (int i = 0; i < scores.length; i++) {
26 if(newScore>scores[i]){
27 index = i;
28 break;
29 }
30 }
31 System.out.println("插入成绩的下标是:"+index);
32 for (int i = scores.length - 2; i >= index; i--) {//下标后移
33 scores[i+1]=scores[i];
34 }
35 scores[index] = newScore;
36 System.out.println("插入成绩后的信息是:");
37 for (int i : scores) {
38 System.out.print(i+"\t");
39 }
40 }
41 }