Java中支持next(),nextLine(),nextInt(),nextDouble(),nextLong()等,但是没有nextChar().
接下来介绍两种输入字符的方法
1)next().charAt(0)
Scanner input = new Scanner(System.in); char c = input.next().charAt(0); System
.out.println(c);
2)next()
Scanner input = new Scanner(System.in); String c = input.next(); System.out.
println(c);
其实不难发现,二者都是把字符当作字符串来输入的。
第一种方法其实是把String字符串转为一个字符数组,next()charAt(0)括号中的’0‘的意思就是输出字符串数组中下标为0的字符。
Scanner input = new Scanner(System.in); //hello char c = input.next().charAt(0)
; //h System.out.println(c);
第二种方法则完全是把字符当作字符串来使用,如果你要进行一些对字符的操作那就需要把String强制转换为char了
Scanner input = new Scanner(System.in); String c = input.next(); System.out.
println(c); //强制转换 byte word= (byte) c, ```

技术
今日推荐
PPT
阅读数 131
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信