博客
关于我
用户交互的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实现Secant method割线法算法(附完整源码)
    查看>>
    Objective-C实现segment tree段树算法(附完整源码)
    查看>>
    Objective-C实现segmented sieve分段筛算法(附完整源码)
    查看>>
    Objective-C实现selection sort选择排序算法(附完整源码)
    查看>>
    Objective-C实现sha1算法(附完整源码)
    查看>>
    Objective-C实现sha256算法(附完整源码)
    查看>>
    Objective-C实现shell sort希尔排序算法(附完整源码)
    查看>>
    Objective-C实现sherman morrison公式算法(附完整源码)
    查看>>
    Objective-C实现ShorAlgorithm肖尔算法 (附完整源码)
    查看>>
    Objective-C实现shortest job first短作业优先算法(附完整源码)
    查看>>
    Objective-C实现shortestCommonSupersequence最短公共超序列算法(附完整源码)
    查看>>
    Objective-C实现sierpinski triangle谢尔宾斯基三角形算法(附完整源码)
    查看>>
    Objective-C实现sieve of Eratosthenes埃拉托色尼筛法算法(附完整源码)
    查看>>
    Objective-C实现SieveOfEratosthenes埃拉托色尼筛法打印所有素数算法(附完整源码)
    查看>>
    Objective-C实现sieveOfEratosthenes埃拉托色尼筛法求素数算法 (附完整源码)
    查看>>
    Objective-C实现sieveOfEratosthenes埃拉托色尼筛选法算法(附完整源码)
    查看>>
    Objective-C实现sigmoid函数功能(附完整源码)
    查看>>
    Objective-C实现Sigmoid函数算法(附完整源码)
    查看>>
    Objective-C实现similarity search相似性搜索算法(附完整源码)
    查看>>
    Objective-C实现simple binary search简单的二分查找算法(附完整源码)
    查看>>