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

本文介绍了一种在已排序成绩数组中插入新成绩并保持降序的方法。通过用户输入新成绩,程序查找合适位置并调整数组,确保成绩列表仍按降序排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值