首页 > 技术文章 > java输入输出

StarZhai 2019-03-22 13:39 原文


格式1:Scanner sc = new Scanner (new BufferedInputStream(System.in))
格式2:Scanner sc = new Scanner (System.in);
在读入数据量大的情况下,格式1的速度会快些。
读一个整数: int n = sc.nextInt();
读一个字符串:String s = sc.next();
读一个浮点数:double t = sc.nextDouble();
读一整行: String s = sc.nextLine();
判断是否有下一个输入可以用sc.hasNext()或sc.hasNextInt()或sc.hasNextDouble()或sc.hasNextLine()

public class Main{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
                
                int n = sc.nextInt();    //读一个整数: 
                String s1 = sc.next();//读一个字符串:
                double t = sc.nextDouble(); //读一个浮点数:
                sc.nextLine();                    //用于清空输入缓存区
                String s2 = sc.nextLine();    //读一整行
                System.out.println("整数为: "+n+""+"字符串为: "+s1+"\t"+"浮点数为: "+t+"\t"+"一整行为: "+s2+"\t");
//                判断是否有下一个输入可以用sc.hasNext()或sc.hasNextInt()或sc.hasNextDouble()或sc.hasNextLine()

    }

参考https://blog.csdn.net/qq_36084640/article/details/79366846#输入输出

推荐阅读