zoukankan      html  css  js  c++  java
  • 【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+"	");
    39         }
    40     }
    41 }
  • 相关阅读:
    157 判断字符串是否没有重复字符
    53 翻转字符串
    671 循环单词
    8 旋转字符串
    39 恢复旋转字符串
    6 合并排序数组 Ⅱ
    64 合并排序数组
    60 搜索插入位置
    141 x的平方根
    TCSRM 593 div2(1000)(dp)
  • 原文地址:https://www.cnblogs.com/yanglanlan/p/11134047.html
Copyright © 2011-2022 走看看