[英]Scan Double doesn't work with try-catch
here's my code: 这是我的代码:
public static void setPriority(List<Performer> listperformer)
{
Scanner localsc = new Scanner(System.in);
if(listperformer.isEmpty() == true)
{
System.out.println("- empty -");
return;
}
else
{
System.out.println("ID:");
int id = localsc.nextInt();
if(listperformer.size() < id || id < 0)
{
System.out.println("Invalid input!");
return;
}
else
{
double priority = 1.0;
try{
System.out.println("Priority:");
priority = localsc.nextDouble();
listperformer.get(id).setPriority(priority);
}catch(InputMismatchException e)
{
System.err.println("Invalid input!");
}
}
}
When I want to scan something like 2,7 it works, but if i put instead of the comma a dot, it leads to the "Invalid input!" 当我想扫描类似2,7的东西时,它可以工作,但是如果我用逗号代替逗号而不是点,则会导致“输入无效!”。 output. 输出。
check if you need to use US locale 检查是否需要使用美国语言环境
Scanner localsc = new Scanner(System.in);
localsc.useLocale(Locale.US);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.