【0703作业】一组成绩降序排列,插入数值

 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 }

 

转载于:https://www.cnblogs.com/yanglanlan/p/11134047.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值