1.Scanner类的应用:
(1)使用scanner类时,要创建一个对象:
Scanner input = new Scanner(System.in)
这里指的是控制台输入流。需要注意的是,Scanner在不使用时要关闭,即
input.close();
ps:当代码中需要多次使用Scannner创建输入流时,不要随意关闭它,否则会造成错误。
(2)如果用Scanner定义了输入流input后,尽量不免使用
input.nextLine();
nextLine()函数对于从控制台输入的变量不会进行判断,经常会出错,尤其是在输入的数据是数字时,例如:
System.out.println("请输入新密码,不得低于6位数");
String newcode1 = input.nextLine();
System.out.println("请再次输入新密码,不得低于6位数");
String newcode2 = input.nextLine();
上述代码在执行之后,会显示:
请输入新密码,不得低于6位数
请再次输入新密码,不得低于6位数
只能得到一次输入的机会。
2 GregorianCalendar类获取当前时间
GregorianCalendar d = new GregorianCalendar();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dateFormat.format(d.getTime());
程序运行后就会的到系统当前时间,并按照“yyyy-MM-dd HH:mm:ss”的格式打印出来3 实现在windows的换行
实现换行应该用“\r\n”