我正在为一个程序编写代码,其中用户输入2到4个数字,最长可以是2个小数位。但是,在用户输入这些数字之前,他们必须输入井号(例如,#12.34)。我想知道如何检查输入的双精度值是否以井号开头,以及用户是否忘记输入,以再次提示他们再次输入。到目前为止,我使用的是String值和'.startsWith()'代码,但后来我发现String值使程序的其余部分无法进行编码,因此我想将其保留为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。
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("#",""));
对于程序的其余部分,请使用value
。我认为现在应该可以进行编码了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句