博客
关于我
用户交互的Scanner对象
阅读量:63 次
发布时间:2019-02-25

本文共 1587 字,大约阅读时间需要 5 分钟。

Java Scanner?????

????

Scanner??Java???????????????????Scanner????????????????????????????????????

1. ???Scanner

Scanner scanner = new Scanner(System.in);

2. ??????

next()??

next()????????????????????????????hasNext()??????????

if (scanner.hasNext()) {    String str = scanner.next();    System.out.println("???????" + str);}

nextLine()??

nextLine()???next()???????????????????????????

if (scanner.hasNextLine()) {    String str = scanner.nextLine();    System.out.println(str);}

??

  • next()????????????????????
  • nextLine()????????????????????

????

Scanner??????????????????????????????????????????

??

Scanner scanner = new Scanner(System.in);System.out.println("?????");if (scanner.hasNextInt()) {    int i = scanner.nextInt();    System.out.println("?????" + i);} else {    System.out.println("????????");}System.out.println("??????");if (scanner.hasNextFloat()) {    float f = scanner.nextFloat();    System.out.println("???" + f);} else {    System.out.println("????????");}

????

????

public static void main(String[] args) {    Scanner scanner = new Scanner(System.in);    double sum = 0;    int m = 0;    while (scanner.hasNextDouble()) {        double nextDouble = scanner.nextDouble();        m++;        sum += nextDouble;        System.out.println("????" + m + "????????sum=" + sum);    }    System.out.println(m + "?????:" + sum);    System.out.println(m + "???????:" + sum/m);    scanner.close();}

????

  • ???????hasNextDouble()?????????????????????
  • ?????????????sum????
  • ??????????????????
  • ?????????scanner.close()?????
  • ?????????????????????????????????????????????????????????????

    转载地址:http://erv.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现对称矩阵压缩存储(附完整源码)
    查看>>
    Objective-C实现寻找Find Lcm最小公倍数算法(附完整源码)
    查看>>
    Objective-C实现寻找HCF算法(附完整源码)
    查看>>
    Objective-C实现寻找无向图的关节点Articulation Points算法(附完整源码)
    查看>>
    Objective-C实现寻找欧拉路径/回路(附完整源码)
    查看>>
    Objective-C实现导弹跟踪算法(附完整源码)
    查看>>
    Objective-C实现将 b 除以模 n 的有效算法(附完整源码)
    查看>>
    Objective-C实现将 base64 字符串转换为字节数组算法(附完整源码)
    查看>>
    Objective-C实现将两个给定的字符串以O(n)的时间复杂度排列在一个字符串中算法(附完整源码)
    查看>>
    Objective-C实现将位转换为浮点数bitsToFloat算法(附完整源码)
    查看>>
    Objective-C实现将列表向右旋转 k 个位置算法(附完整源码)
    查看>>
    Objective-C实现将字符串中大写字母转换为小写字母(附完整源码)
    查看>>
    Objective-C实现将字符串从一个基转换为另一个基算法(附完整源码)
    查看>>
    Objective-C实现将字符串小写转大写算法(附完整源码)
    查看>>
    Objective-C实现将字节数组转换为 base64 编码算法(附完整源码)
    查看>>
    Objective-C实现将彩色图像转换为负片算法(附完整源码)
    查看>>
    Objective-C实现将无符号整数n变成成d进制表示的字符串s(附完整源码)
    查看>>
    Objective-C实现将给定的 utf-8 字符串编码为 base-16算法(附完整源码)
    查看>>
    Objective-C实现将给定的字符串编码为 base32算法(附完整源码)
    查看>>
    Objective-C实现小根堆(附完整源码)
    查看>>