zoukankan      html  css  js  c++  java
  • Scanner的nextInt() nextLine() next()方法

    Scanner类的nextInt() nextLine()  next()方法是以不同的标识作为结束符的

    nextInt() 和 next()是以空格作为结束符

    nextLine() 是以 换行符作为结束符

    当我们在使用 nextInt() 或者 next()我们一般是输入内容后  回车 结束,,,这个时候我们接受到的内容是空格之前的;如下

    Scanner sc=new Scanner(System.in);
    System.out.println("请输入数字");
    int i=sc.nextInt();
    System.err.println(i);

    Scanner sc=new Scanner(System.in);
    System.out.println("请输入数字");
    String str=sc.next();
    System.err.println(str);

    此时  当我们调用了nextInt() 或者 next()   再调用了nextLine()方法

    Scanner sc=new Scanner(System.in);
    System.out.println("请输入第一行");
    String str=sc.next();
    System.err.println(str);
    System.out.println("请输入第二行");
    String str2=sc.next();
    System.err.println(str2);

    结果这边刚打印输入提示就直接输出数据了。

    这是由于 这边第一行输入hello  world然后回车,str使用的next()方法只能识别到空格前的字符串,它是以空格作为结束符的。当我们调用nextLine()方法的使用sc对象就已经获得了world加上回 符号,由于nextLine()方法是以回车座位结束符,这边就直接将world作为扫描结果赋值给了str2

  • 相关阅读:
    洛谷 P2872 [USACO07DEC]道路建设Building Roads
    cogs 29. 公路建设
    cogs 7. 通信线路
    cogs 2478. [HZOI 2016]简单的最近公共祖先
    洛谷 P1342 请柬
    洛谷 P1186 玛丽卡
    洛谷 P1491 集合位置
    启动、停止、重启服务
    洛谷——P1025 数的划分
    洛谷——P3368 【模板】树状数组 2
  • 原文地址:https://www.cnblogs.com/pcyiren/p/8889808.html
Copyright © 2011-2022 走看看