scanner控制行输入

输入样式:
2
3 2
5 6 7
8 2
238 153 223 247 111 252 253 247
输出样式:
4
9

1. nextInt()方法读取一个数后就按任意字符都会结束;

2. nextLine()方法读取一行,以enter结束;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = Integer.parseInt(scanner.nextLine());//单个数字输入
        while (T > 0) {
            String input = scanner.nextLine();//整行输入
            int n = Integer.valueOf(input.split(" ")[0]);
            int k = Integer.valueOf(input.split(" ")[1]);
            input = scanner.nextLine();
            int[] nums = new int[n];
            String[] strings = input.split(" ");
            for (int i = 0; i < n; i++) {
                nums[i] = Integer.valueOf(strings[i]);
            }
            T--;
            //调用
            System.out.println();
        }
        scanner.close();
    }

你可能感兴趣的:(scanner控制行输入)