[英]Why can I only Input a “1,5” but not a “1.5”
I coded a little program to calculate a tax, but I have a problem. 我编写了一个小程序来计算税款,但是我遇到了问题。 The problem multiplies the number that I put in by 0.9975, but I can only put in a double number with a "," and not with a dot ".".
该问题将我输入的数字乘以0.9975,但我只能输入带有“,”而不是点“。”的双精度数字。
import java.util.Scanner;
public class Bitrex {
public static void main(String[] args) {
Scanner Bitcoin = new Scanner (System.in);
double num1;
double num2=0.9975;
double answer;
System.out.println("Enter Expected Price: ");
num1 = Bitcoin.nextDouble();
answer = num1 * num2;
System.out.println(answer);
} // end of main
}
You probably are in Germany and Scanner
uses the default Locale
, that is, ,
is used as the decimal separator for parsing. 您可能在德国,并且
Scanner
使用默认的Locale
,即,
用作解析的十进制分隔符。 See the Javadoc of Scanner . 请参阅Scanner的Javadoc 。
You should set the Locale
you want to use like in 您应该像下面那样
Locale
要使用的Locale
...
Scanner Bitcoin = new Scanner (System.in);
Bitcoin.useLocale(Locale.US); // or Locale.ROOT or new Locale("pt", "BR")
...
(do not forget import java.util.Locale;
) (不要忘记
import java.util.Locale;
)
Eventually it is better to set the default Locale for the whole application using Locale.setDefault(Locale.ROOT);
最终,最好使用
Locale.setDefault(Locale.ROOT);
为整个应用程序设置默认语言环境Locale.setDefault(Locale.ROOT);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.