标准输入输出
输入
标准输入流System.in
Scanner s = new Scanner(System.in);
常用的next()方法系列:
nextInt():输入整数
nextLine():输入字符串
nextDouble():输入双精度数
next():输入字符串(以空格作为分隔符)
Scanner s=new Scanner(System.in);
System.out.println("输入一个整数:");
int i = s.nextInt();
System.out.println("输入一个双精度浮点数:");
double d = s.nextDouble();
System.out.println("输入一个字符串:");
String sc = s.next(); //也可以用nextLine()去掉next()支持传入空格
s.close(); //不关闭会有警告
注意
用 next() 方法:会因空格终止输入。
用 nextLine() 方法:这个方法不会因为空格终止。两种方法都终止于回车。
输出
(1)一般标准输出字符流
System.out.print("输入的整数为:"+i);//不换行打印
System.out.println("输入的浮点数为:"+d);
System.out.printf("输入的字符串为:%s\n", sc);//按格式输出
(2)输出字节流
System.out.write(2222); //字节输出,用着不方便所以不常用。
//输出后看不到,如果想要显示,如下
String str = "Hello World";
for(int i=0; i<str.length(); i++)
System.out.write(str.charAt(i));
System.out.flush();
原文转自:https://www.cnblogs.com/StarZhai/p/15967879.html