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

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();
    }
}
全部评论

相关推荐

09-14 17:23
门头沟学院
故事和酒66:所以说副业很重要,程序员干到40岁,再怎么也赚300万了,吃吃利息也够活下去
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
2
分享

创作者周榜

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