java - 如何检查 double 值是否包含特殊字符

标签 java double

我正在为一个程序编写代码,其中用户输入 2 到 4 个数字,最多可以有 2 位小数。然而,在用户输入这些数字之前,他们必须输入英镑符号,例如#12.34.我想知道如何检查输入的 double 值是否以井号开头,以及如果用户忘记输入它,则重新提示他们再次输入。到目前为止,我使用的是字符串值和“.startsWith()”代码,但后来我发现字符串值使程序的其余部分无法编码,所以我想将其保留为 double 值。这是我目前的代码,但希望更改为 double :

String input;
System.out.print("Enter the 4 numbers with a pound key: ");
input = keyboard.next();
while (!input.startsWith("#")) {
 System.out.print("Re-enter the 4 numbers with a pound key: ");
 input = keyboard.next();
}

我想用前面提到的 double 替换字符串。

最佳答案

String input;
System.out.print("Enter the 4 numbers with a pound key: ");
input = keyboard.next();
while (!input.startsWith("#")) {
 System.out.print("Re-enter the 4 numbers with a pound key: ");
 input = keyboard.next();
}
// if your excution reaches here. then it means the values entered by user is starting from '#'.
String temp = input;
double value = Double.parseDouble(temp.replace("#",""));

对于程序的其余部分,请使用。我想现在应该可以编码了。

关于java - 如何检查 double 值是否包含特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36282692/

相关文章:

java - 二进制文件读取不正确

c++ - 在循环中确定 DBL_MIN

c++ - Visual C++ double 与 C++ double

c++ - 具有 double 的二进制文件大小不一致 (C/C++)

java - hibernate、stackoverflow 与特定实体映射

java - 如何在 Liferay 中获取范围值

java - getJSONObject 方法有问题

java - Spring中HTTP请求后是否需要清除MDC

c# - 将 double 转换为 int,它是四舍五入还是只是去掉数字?

c - 如何在 C 中的二维双数组中读取 .pgm 图像文件