博客
关于我
用户交互的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实现countSetBits设置位的数量算法(附完整源码)
    查看>>
    Objective-C实现currency converter货币换算算法(附完整源码)
    查看>>
    Objective-C实现cycle sort循环排序算法(附完整源码)
    查看>>
    Objective-C实现data transformations数据转换算法(附完整源码)
    查看>>
    Objective-C实现datamatrix二维码识别 (附完整源码)
    查看>>
    Objective-C实现DateToDay 方法算法(附完整源码)
    查看>>
    Objective-C实现DBSCAN聚类算法(附完整源码)
    查看>>
    Objective-C实现DBSCAN聚类算法(附完整源码)
    查看>>
    Objective-C实现decision tree决策树算法(附完整源码)
    查看>>
    Objective-C实现degreeToRadian度到弧度算法(附完整源码)
    查看>>
    Objective-C实现depth first search深度优先搜索算法(附完整源码)
    查看>>
    Objective-C实现DES和3DES加解密算法(附完整源码)
    查看>>
    Objective-C实现des文件加密算法(附完整源码)
    查看>>
    Objective-C实现detectDirectedCycle检测定向循环算法(附完整源码)
    查看>>
    Objective-C实现detectUndirectedCycle检测无向循环算法(附完整源码)
    查看>>
    Objective-C实现deutsch jozsa算法(附完整源码)
    查看>>
    Objective-C实现DFS判断是否是二分图Bipartite算法(附完整源码)
    查看>>
    Objective-C实现DFS遍历或搜索图数据结构算法(附完整源码)
    查看>>
    Objective-C实现Diffie-Hellman算法(附完整源码)
    查看>>
    Objective-C实现Diffie—Hellman密钥交换(附完整源码)
    查看>>