gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:03:00 24 4
gpt4 key购买 nike

我正在为一个程序编写代码,其中用户输入 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com