从控制台读取数据

从控制台读取一个数字


public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    String str = s.nextLine();
    int n = Integer.parseInt(str);
    // int n = s.nextInt();
}

从控制台读取一串数字,可以是一行,也可以是多行,ctrl+c停止输入


public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    ArrayList<Integer> list = new ArrayList<>();
    while (s.hasNextInt())
        list.add(s.nextInt());
}

猜你喜欢

转载自www.cnblogs.com/alyiacon/p/12576824.html