Read integers separated with whitespace into int[] array

前端 未结 10 1933
陌清茗
陌清茗 2020-12-13 19:13

I read line with

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
reader.readLine();

Example input is

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 19:28

     Scanner sc = new Scanner(System.in);
        String n=sc.nextLine();
        String[] no=n.split(" ");
        int sum=0;
        for(String i:no){
            sum+=Integer.parseInt(i);}
        System.out.println(sum);
    

    The Above code is how can you take space separate integer inputs using java programming and perform any task of your choice in my case I have added the numbers.

提交回复
热议问题