Scanner类的nextInt()方法疑问

先上代码
第一个:

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        String line;
        long a;
        long b;
        long c;
        boolean[] booleans = new boolean[count];
        sc.nextLine();
        for (int i = 0; i < count; i++) {
            line = sc.nextLine();
            a = Integer.valueOf(line.split(" ")[0]);
            b = Integer.valueOf(line.split(" ")[1]);
            c = Integer.valueOf(line.split(" ")[2]);
            booleans[i] = (a + b > c);
        }
        for (int i = 0; i < count; i++) {
            System.out.println("Case #" + (i + 1) + ": " + booleans[i]);
        }
    }
}

第二个:

import java.util.Scanner;
public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        boolean[] result = new boolean[n];
        for(int i = 0; i < n; i++)
        {
            long a = sc.nextLong();
            long b = sc.nextLong();
            long c = sc.nextLong();
            if((c-a)<b) result[i] = true;
            else result[i] = false;
        }
        for(int i = 0; i < n; i++)
            System.out.println("Case #" + (i+1) + ": "+result[i]);
    }
}

两段代码几乎一样,但是牛客网上编译运行,第二个完全通过,第一个会报异常,异常出现在int count = sc.nextInt();这一句,如果输入一个非int类型的数据会有异常,我很疑问,第二段代码也有sc.nextInt();为什么不会报异常?求大神解答

#笔试题目##Java#
全部评论
判卷机很奇怪的,一般出现这种问题全部nextLine再去处理字符串比较稳妥
点赞 回复 分享
发布于 2019-08-27 09:13
我也很好奇为什么
点赞 回复 分享
发布于 2019-08-27 08:52
nextInt后不能接着用nextLine
点赞 回复 分享
发布于 2019-08-26 19:08

相关推荐

05-23 20:31
已编辑
武汉大学 Java
内向的柠檬精在研究求职打法:注意把武大标粗标大 本地你俩不是乱杀
点赞 评论 收藏
分享
05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务