zoukankan      html  css  js  c++  java
  • 问题:关于2.3 jmu-Java-02基本语法-03-身份证排序 (9 分)

    输出未能排序
        import java.util.Scanner;
        import java.util.Arrays;
        public class Main {
            public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int n =sc.nextInt();
         sc.nextLine();
                String[] ID =new String[n];
                for (int i=0;i<n;i++) {
                    ID[i]=sc.nextLine();
                }
                for (int i=0;i<n;i++) {
                    for (int j=i;j<n-1;j++) {
                        if (Integer.parseInt(ID[j].substring(6,10))>Integer.parseInt(ID[j+1].substring(6,10))) {
                            String Tmp = ID[j];
                            ID[j] =ID[j+1];
                            ID[j+1]= Tmp;
                            
                        }else if(Integer.parseInt(ID[j].substring(6,10))==Integer.parseInt(ID[j+1].substring(6,10))){
                            if(Integer.parseInt(ID[j].substring(10,12))>Integer.parseInt(ID[j+1].substring(10,12))){
                                String Tmp = ID[j];
                                ID[j]=ID[j+1];
                                ID[j+1]=Tmp;
                            }
                            else if (Integer.parseInt(ID[j].substring(10,12))==Integer.parseInt(ID[j+1].substring(10,12))){
                                 if (Integer.parseInt(ID[j].substring(12,14))>Integer.parseInt(ID[j+1].substring(12,14))) {
                                     String Tmp =ID[j];
                                     ID[j]=ID[j+1];
                                     ID[j+1]=Tmp;
                                 }
                            }
                        }
                    }
                }
                for(;;) {
                    String tmp=sc.nextLine();
                    if (tmp.equals("sort1"))
                    {
                        for(int i=0;i<n;i++) {
                            System.out.println(ID[i].substring(6,10)+"-"+ID[i].substring(10,12)+"-"+ID[i].substring(12,14));
                        }
                        }else if (tmp.equals("sort2")) {
                        for (int i=n-1;i>=0;i--) {
                            System.out.println(ID[i]);
                        }
                    }else{
                        
                        System.out.println("exit");
                        break;
                    }
                }
            }
                        


            }   
  • 相关阅读:
    全排列和几道例题
    NOJ1333: [蓝桥杯2017初赛]Excel地址
    力扣5-最长回文子串-(Manacher算法)
    CF1003E-Tree Constructing-(构造+dfs)
    NOJ1329:[蓝桥杯2017初赛]k倍区间-(前缀和)
    Java 时间
    小魂和他的数列-(离散+二分+树状数组)
    AC自动机入门和几道例题
    java写入加速
    清理 Excel 导出的 HTML 的多余属性
  • 原文地址:https://www.cnblogs.com/zlshy/p/11524090.html
Copyright © 2011-2022 走看看