博客
关于我
用户交互的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/

    你可能感兴趣的文章
    noi 7827 质数的和与积
    查看>>
    NOI-1.3-11-计算浮点数相除的余数
    查看>>
    NOI2010 海拔(平面图最大流)
    查看>>
    NOIp2005 过河
    查看>>
    NOIP2011T1 数字反转
    查看>>
    NOIP2014 提高组 Day2——寻找道路
    查看>>
    noip借教室 题解
    查看>>
    NOIP模拟测试19
    查看>>
    NOIp模拟赛二十九
    查看>>
    Vue3+element plus+sortablejs实现table列表拖拽
    查看>>
    Nokia5233手机和我装的几个symbian V5手机软件
    查看>>
    Non-final field ‘code‘ in enum StateEnum‘
    查看>>
    none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
    查看>>
    None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
    查看>>
    NoNodeAvailableException None of the configured nodes are available异常
    查看>>
    Vue.js 学习总结(16)—— 为什么 :deep、/deep/、>>> 样式能穿透到子组件
    查看>>
    nopcommerce商城系统--文档整理
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NoSQL介绍
    查看>>