这是我的函数,用于控制用户的双输入,但如果用户输入逗号","字符(例如100300(,它只控制整数值,它拒绝接受它,所以我想做以下事情:当函数遇到逗号字符时,"它会将其更改为点字符"然后我想知道现在字符串值是否可以转换为类似于"的双精度;100.300〃;`
public static double controlDoubleInput() {
Scanner scanner = new Scanner(System.in).useDelimiter("n");
String option = scanner.next();
boolean isNotDigit = true;
char[] array = option.toCharArray();
do {
for(char c : array) {
if(Character.isDigit(c)) {
isNotDigit = false;
}
else {
isNotDigit = true;
break;
}
}
if(isNotDigit) {
System.out.println("input mismatch please retry : ");
option = scanner.next();
array = option.toCharArray();
}
} while(isNotDigit);
return Double.parseDouble(option);
}
`
使用本地化。
try (Scanner scanner = new Scanner(System.in).useDelimiter("n")) {
String option = scanner.next();
NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
while (true) {
try {
return nf.parse(option).doubleValue();
} catch (ParseException e) {
System.out.println("input mismatch please retry : ");
option = scanner.next();
}
}
}