扫描双精度不适用于尝试捕获



这是我的代码:

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!");
    }
   }
  }

当我想扫描类似 2,7 的东西时,它可以工作,但如果我放一个点而不是逗号,它会导致"无效输入!

检查是否需要使用美国语言环境

Scanner localsc = new Scanner(System.in);
localsc.useLocale(Locale.US);

最新更新