博客
关于我
JAVA流程控制01——用户交互Scanner和scanner的进阶使用
阅读量:250 次
发布时间:2019-03-01

本文共 506 字,大约阅读时间需要 1 分钟。

JAVA流程控制——用户交互Scanner和scanner的进阶使用

Scanner对象

通过Scanner类的next()与nextLine()获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据

next()与nextLine()的区别

next():

  • 一定要读取到有效字符才可以结束输入
  • 对输入有效字符之前遇到的空白,next()方法会自动将其去掉
  • 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符
  • next()不能得到带有空格的字符串

在这里插入图片描述

在这里插入图片描述

nextLine():

  • 以Enter为结束符,也就是说nextLine()放发返回的是输入回车

之前的所有字符

  • 可以获得空白

在这里插入图片描述

在这里插入图片描述

注意:凡是属于IO(输入输出)流的类如果不关闭会一直占用资源,要要用完就关掉。列如scanner类,使用完后要关闭scanner.close();

拓展:不加判断直接使用scanner类来获取输入的字符

在这里插入图片描述

Scanner的进阶使用

输入一个数判断输入的是整数还是小数

在这里插入图片描述

我们可以输入多个数字,并且求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束并输出执行结果:

在这里插入图片描述

转载地址:http://oqov.baihongyu.com/

你可能感兴趣的文章
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
nginxWebUI runCmd RCE漏洞复现
查看>>
nginx_rtmp
查看>>
Nginx、HAProxy、LVS
查看>>
nginx一些重要配置说明
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx与Tengine安装和使用以及配置健康节点检测
查看>>
Nginx中使用expires指令实现配置浏览器缓存
查看>>