Java Scanner nextDouble() 方法

2024 年 11 月 6 日 | 阅读 2 分钟

nextDouble() 是 Java Scanner 类的一个方法,用于将下一个输入标记扫描为 double 类型。如果转换成功,则扫描器会跳过已匹配的输入。

语法

以下是 nextDouble() 方法的声明

参数

此方法不接受任何参数。

返回值

nextDouble() 方法返回扫描的 double 类型输入。

异常

InputMismatchException - 如果下一个标记不匹配 Float 正则表达式或超出范围,则会抛出此异常。

NoSuchElementException - 如果输入耗尽,将抛出此异常。

IllegalStateException - 如果在 Scanner 关闭后调用,则会抛出此异常。

兼容版本

Java 1.5 及以上版本

示例 1

输出

Enter the amount: 
213734
Enter the Total Balance: 
5684566.856
Left Balance is: 5470832.356

示例 2

输出

Not Found Double:Hello
Not Found Double:World!
Found Double:12.0
Not Found Double:+
Found Double:13.0
Not Found Double:=
Found Double:15.0
Not Found Double:2.123

示例 3

输出

Enter value: 375437.565
Your entered Double value: 375437.565

示例 4

输出

Enter the numeric value : 12345
Double value : 12345.0 
Twice value : 24690.0