题解 | #字符串排序#

字符串排序

http://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723

next()后面一定不要用nextLine,否则输入必存在问题 解决办法:1.该nextLine()为next() 2.在next()后再加一个nextLine()

import java.util.*;
public class Main{
    public static void main(String args[]) {
    
        Scanner sc=new Scanner(System.in);
            int n =sc.nextInt();
            String[] strs=new String[n];
            for(int i=0;i<n;i++){
                strs[i]=sc.next();//这里不要用nextline()
                /**
                nextLine()不能用在nextInt()的后面!!!!
                //nextLine()自动读取了被next()去掉的Enter作为他的结束符,
                所以没办法从键盘输入值。
                */
           }
             Arrays.sort(strs);//Arrays.sort()自带排序
             for(String str:strs) {
                System.out.println(str);
            
        }
    }
}
全部评论

相关推荐

牛客316659795号:不是,证明hr初筛已经过了,要投给部门筛一遍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务