Java Scanner(cmd数据输入)

393 阅读1分钟
// 导库
import java.util.Scanner;

public class test {
  public static void main(String[] args) {
    // 创建对象
    Scanner sc = new Scanner(System.in);
    // 接受数据,这里设定为只接受数字,非数字则会报错
    int x = sc.nextInt();
    // 多次输出则在多写个
    // int x2 = sc.nextInt();
    // 输出数据
    System.out.println(x);
    // 关闭输入,如果不关闭会报错 Resource leak: 'sc' is never closed
    sc.close();
  }  
}

案例代码

image.png