英文:
In this code when i write double value in input an error occur
问题
当我在输入中插入一个双精度(double)值时,会出现错误,比如插入3.86。
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
double sum = 0.0, value;
boolean positivenum = true;
while (positivenum) {
System.out.println("输入您的数字或输入负数退出");
value = reader.nextDouble();
if (value < 0.0)
positivenum = false;
else
sum += value;
System.out.printf("您的总和为:" + sum);
}
}
英文:
When I insert a double value in input an error occur such as insert 3.86
public static void main (String args[])
Scanner reader = new Scanner (System.in);
double sum = 0.0 , value;
boolean positivenum = true ;
while(positivenum){
System.out.println("Enter your num or neg to exit");
value = reader.nextDouble();
if (value < 0.0)
positivenum = false;
else
sum += value;
System.out.printf("Your sumtion is " + sum);
}
}
答案1
得分: 2
你需要输入3,86
而不是3.86
,使用逗号,
代替点号.
,否则会出现java.util.InputMismatchException
错误。
如果你想要使用点号.
作为小数分隔符,你可以按照以下方式自定义你的Scanner
:
Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
现在它应该可以使用点号.
了。
编辑:
不要忘记导入Locale:import java.util.Locale;
英文:
You need to enter 3,86
instead of 3.86
, use ,
instead of .
, or you will get java.util.InputMismatchException
.
If you like to use .
for double separation you can customize your Scanner
like below:
Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
Now it should work with .
.
EDIT:
Don't forget to import Locale: import java.util.Locale;
答案2
得分: 1
按我在评论中已经说过的,这是因为在将字符串传递给 Double::parseDouble 之前,字符串会根据当前的本地化进行转换。
你可以在你使用的 Scanner::nextDouble 方法的 JavaDoc 中读到这一点:
[...] 通过删除所有与语言环境相关的前缀、分组分隔符和与语言环境相关的后缀,然后通过 Character.digit 将非 ASCII 数字映射为 ASCII 数字,如果存在语言环境相关的负数前缀和后缀,则添加负号(-),然后将结果字符串传递给 Double.parseDouble。
所以正如我在评论中也提到的,如果你使用逗号作为小数分隔符,它很可能会起作用。要改变这个行为,你可以改变当前的本地化。
你也可以在 Scanner 类的 JavaDoc 中找到这些信息:
本地化数字
此类的实例能够以标准格式和扫描仪语言环境的格式扫描数字。扫描仪的初始语言环境是 Locale.getDefault(Locale.Category.FORMAT) 方法返回的值;可以通过 useLocale() 方法进行更改。无论以前是否更改,reset 方法都将将扫描仪的语言环境的值重置为初始语言环境。
因此,你可以简单地将扫描仪的语言环境设置为美国:
reader.useLocale(Locale.US);
这将使用“.”作为小数分隔符。
由于 Scanner::useLocale 会返回扫描仪对象,你可以链式调用它,并在同一行中与扫描仪的构造一起使用:
Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
从版本 1.6 开始,你还可以使用 ROOT 语言环境。在这种情况下,我更喜欢使用它,因为根据文档:
用于根语言环境的常量。根语言环境的语言、国家和变体为空("")字符串。这被视为所有语言环境的基本语言环境,并且用作语言/国家中立语言环境的区域敏感操作。
所以这是你修改过的代码:
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
reader.useLocale(Locale.ROOT);
double sum = 0d, value;
boolean positivenum = true;
while (positivenum) {
System.out.println("Enter your num or neg to exit");
value = reader.nextDouble();
if (value < 0.0)
positivenum = false;
else
sum += value;
System.out.println("Your sum is " + sum);
}
}
你可能需要为 Locale 类添加一个导入语句:
import java.util.Locale;
英文:
As I already said in my comment, this is because the string is converted according to the current localisation before it gets passed to Double::parseDouble.
You can read that in the JavaDoc of the Scanner::nextDouble method you are using:
> [...] by removing all locale specific prefixes, group separators, and localespecific suffixes, then mapping non-ASCII digits into ASCIIdigits via Character.digit, prepending anegative sign (-) if the locale specific negative prefixes and suffixeswere present, and passing the resulting string to Double.parseDouble.
So as I also mentioned in the comment, it'll most likely work when you use a comma as decimal separator. To change this behaviour, you can change the current Localisation.
You can find this information in the JavaDoc of the Scanner class as well:
> Localized numbers
> An instance of this class is capable of scanning numbers in the standardformats as well as in the formats of the scanner's locale. A scanner's initial locale is the value returned by the Locale.getDefault(Locale.Category.FORMAT) method; it may be changed via the useLocale() method. The reset method will reset the value of thescanner's locale to the initial locale regardless of whether it waspreviously changed.
So what you can do is simply set the Scanners locale to US:
reader.useLocale(Locale.US);
And it will work with "." instead of "," as decimal separator.
As Scanner::useLocale will return the scanner object, you can chain this call and use it in the same line of the construction of the scanner:
Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
Since version 1.6 you can also use the ROOT-locale. I would prefer it in this case, because it's meant to be country-neutral, as it's stated in the documentation:
> Useful constant for the root locale. The root locale is the locale whoselanguage, country, and variant are empty ("") strings. This is regardedas the base locale of all locales, and is used as the language/countryneutral locale for the locale sensitive operations.
So here's your modified code:
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
reader.useLocale(Locale.ROOT);
double sum = 0d, value;
boolean positivenum = true;
while (positivenum) {
System.out.println("Enter your num or neg to exit");
value = reader.nextDouble();
if (value < 0.0)
positivenum = false;
else
sum += value;
System.out.println("Your sumtion is " + sum);
}
}
You may need to add an import statement for the Locale-class:
import java.util.Locale;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论