Java:.nextLine() 和 .nextDouble() 区别 [英] Java: .nextLine() and .nextDouble() differences

查看:19
本文介绍了Java:.nextLine() 和 .nextDouble() 区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Java API,因为我对 .nextLine().nextDouble() 之间的区别有疑问.在 API 中,它对 .nextLine() 说:

I was reading the API for Java because I had a question on the difference between .nextLine() and .nextDouble(). In the API, it says this for .nextLine():

将此扫描器推进到当前行并返回被跳过的输入.此方法返回当前行的其余部分,不包括末尾的任何行分隔符.位置设置为下一行的开头."

"Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line."

很简单……它会跳过您所在的当前行,然后返回您刚刚跳过的行.但是对于 .nextDouble(),它是这样写的:

Easy enough...it skips the current line you are on and then returns the line you just skipped. But for .nextDouble(), it says this:

将输入的下一个标记扫描为双精度值.如果下一个标记无法转换为有效的双精度值,则此方法将抛出 InputMismatchException.如果转换成功,扫描器将通过匹配的输入."

"Scans the next token of the input as a double. This method will throw InputMismatchException if the next token cannot be translated into a valid double value. If the translation is successful, the scanner advances past the input that matched."

那么这是否意味着 .nextDouble() 不会跳到新行,而是只读取到双精度值的末尾,然后在行尾停止?这将帮助我理解我现在正在处理的程序.谢谢你们!

So does this mean that .nextDouble() does not skip to a new line and that it only reads until the end of the double and then stops at the end of the line? This would help me in understanding a program I'm working on now. Thanks guys and gals!

推荐答案

nextDouble()

只从构成双精度的输入流中读取接下来的 4 个字节,并且不会前进到下一行,而 nextLine() 会.另外我认为你需要明白一个新行是由字符串 ' ' 指定的,显然一个数字不会包含换行符,所以 'nextDouble()' 不会'前进'到下一行.

only reads the next 4 bytes from the input stream which make up the double and does not advance to the next line, whereas nextLine() does. Also i think you need to understand that a new line is designated by the string ' ', and obviously a number will not contain a linebreak, so 'nextDouble()' will not 'advance' to the next line.

Razvan 是对的

这篇关于Java:.nextLine() 和 .nextDouble() 区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆