博客
关于我
用户交互的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实现哈希查找(附完整源码)
    查看>>
    Objective-C实现哈希表算法(附完整源码)
    查看>>
    Objective-C实现哥德巴赫猜想(附完整源码)
    查看>>
    Objective-C实现唯一路径问题的动态编程方法的算法(附完整源码)
    查看>>
    Objective-C实现四舍五入(附完整源码)
    查看>>
    Objective-C实现四阶龙格库塔法(附完整源码)
    查看>>
    Objective-C实现四阶龙格库塔法(附完整源码)
    查看>>
    Objective-C实现回调实例(附完整源码)
    查看>>
    Objective-C实现图书借阅系统(附完整源码)
    查看>>
    Objective-C实现图像二维熵的图像信号丢失检测(附完整源码)
    查看>>
    Objective-C实现图像去雾算法(附完整源码)
    查看>>
    Objective-C实现图层混合算法(附完整源码)
    查看>>
    Objective-C实现图片erosion operation侵蚀操作算法(附完整源码)
    查看>>
    Objective-C实现图片的放大缩小(附完整源码)
    查看>>
    Objective-C实现图片腐蚀(附完整源码)
    查看>>
    Objective-C实现图片膨胀(附完整源码)
    查看>>
    Objective-C实现图的邻接矩阵(附完整源码)
    查看>>
    Objective-C实现圆球的表面积和体积(附完整源码)
    查看>>
    Objective-C实现在Regex的帮助下检查字谜算法(附完整源码)
    查看>>