记录笔试遇到的输入输出问题

1. 整数,第一行输入n,第二行输入n个数

图片说明

//类名一定要写成Main
class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()) {
            int n = sc.nextInt();
            //长度n
            int[] arr = new int[n];
            //n个数
            for(int i=0;i<n;i++) {
                arr[i] = sc.nextInt();
            }
        }
    }
}

2. 整数,中间有逗号“,”分隔

图片说明

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] strArr = s.split(",");
        int[] arr = new int[strArr.length];
        for (int i = 0; i < strArr.length; i++) {
            arr[i] = Integer.parseInt(strArr[i]);
        }
        System.out.println();
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 12:05
点赞 评论 收藏
分享
zzzzhz:兄弟你先猛猛投简历至少三百家,能约到面试就去面。最近可以速成智能小车,智慧家居烂大街的项目,不需要自己写,只需要把里面的代码讲解看明白就行。把其中涉及到的八股文都拿出来单独背一下,我去年找工作就一个智能小车智慧家居找了10k差不多。
点赞 评论 收藏
分享
评论
1
2
分享

创作者周榜

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