Java 获取多行输入

  • 数组,已知长度
public class test_0806_1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int lent = input.nextInt();
        int[] arr = new int[lent];
        for (int i = 0; i < lent; i++) {
            arr[i] = input.nextInt();
        }
        for (int k : arr) {
            System.out.print(k + " ");
        }
    }
}

  • 整行字符串,已知行数
public class test_0806_2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int t_length = input.nextInt();
        String[] aa = new String[t_length];
        input.nextLine();
        for (int i = 0; i < t_length; i++) {
            aa[i] = input.nextLine();
        }
        for (String str1 : aa) {
            System.out.println(str1);
        }
    }
}

Java 获取多行输入_第1张图片

你可能感兴趣的:(Java)