Scanner类介绍及举例
详细的介绍见java的开发文档,本文进行几个举例:
举例代码如下,除了string类型,其他数据类型都是调用方法next+数据类型,如nextInt():
public static void main(String[] args) { //创建对象并初始化 Scanner data = new Scanner(System.in); //整形变量 System.out.println("please input a number:"); int i = data.nextInt(); System.out.println("number:" + i); //Double data = new Scanner(System.in); System.out.println("please input a Double:"); double d= data.nextDouble(); System.out.println("Double:" + d); // String data = new Scanner(System.in); System.out.println("please input a String:"); String s= data.next(); System.out.println("String:" + s); // boolean data = new Scanner(System.in); System.out.println("please input a boolean:"); boolean b= data.nextBoolean(); System.out.println("boolean:" + b); }