Java常用的输入输出

一、输入

1.使用Scanner类:

(1)使用java.util包。 import java.util.*;

(2)构造Scanner类对象,它附属于标准输入流System.in。 Scanner s = new Scanner(System.in);

(3)常用的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();  //不关闭会有警告

 

二、输出

 

System.out.println("输入的整数为:"+i);//不换行打印
System.out.println("输入的浮点数为:"+d);
System.out.printf("输入的字符串为:%s\n", sc);//按格式输出


System.out.write(2222); //字节输出,用着不方便所以不常用。
posted @ 2022-03-05 14:11  StarZhai  阅读(2993)  评论(0编辑  收藏  举报