博客
关于我
用户交互的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实现3n+1猜想(附完整源码)
    查看>>
    Objective-C实现9x9乘法表算法(附完整源码)
    查看>>
    Objective-C实现9×9二维数组数独算法(附完整源码)
    查看>>
    Objective-C实现A*(A-Star)算法(附完整源码)
    查看>>
    Objective-C实现A-Star算法(附完整源码)
    查看>>
    Objective-C实现abbreviation缩写算法(附完整源码)
    查看>>
    Objective-C实现ABC人工蜂群算法(附完整源码)
    查看>>
    Objective-C实现activity selection活动选择问题算法(附完整源码)
    查看>>
    Objective-C实现AC算法(Aho-Corasick) 算法(附完整源码)
    查看>>
    Objective-C实现adaboost算法(附完整源码)
    查看>>
    Objective-C实现Adler32算法(附完整源码)
    查看>>
    Objective-C实现AES算法(附完整源码)
    查看>>
    Objective-C实现AffineCipher仿射密码算法(附完整源码)
    查看>>
    Objective-C实现aliquot sum等分求和算法(附完整源码)
    查看>>
    Objective-C实现all combinations所有组合算法(附完整源码)
    查看>>
    Objective-C实现all permutations所有排列算法(附完整源码)
    查看>>
    Objective-C实现all subsequences所有子序列算法(附完整源码)
    查看>>
    Objective-C实现AlphaNumericalSort字母数字排序算法(附完整源码)
    查看>>
    Objective-C实现alternate disjoint set不相交集算法(附完整源码)
    查看>>
    Objective-C实现alternative list arrange备选列表排列算法(附完整源码)
    查看>>