ACM模式关于java输入的坑!

今天在牛客网做题,题目是先输入数字n,然后输入n个串,结果每次能够输入的串都是n-1,后来发现,nextInt不会自动换行,因此提供一些解决方法。
方法一:以串读入,转为int


        Scanner sc = new Scanner(System.in);
        Integer num = Integer.parseInt(sc.nextLine());
        String[] strArr = new String[num];
        for (int i=0; i<num; i++) {
            strArr[i] = sc.nextLine();
        }



方法二:在nextInt后读入一个换行

        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        sc.nextLine();
        String[] strArr = new String[num];
        for (int i=0; i<num; i++) {
            strArr[i] = sc.nextLine();
        }

————————————————
版权声明:本文为CSDN博主「CURD工程师」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_41432270/article/details/89947415

posted @ 2021-10-22 00:21  DawnHouse  阅读(309)  评论(0)    收藏  举报