我试图弄清楚如何在Java中使用定界符。基本上,我试图将这样的输入解析:“(x1,x2)”成双精度值。当我将变量分配给下一个double时,下一个整数将被计为double而不是定界符之间的值。如何分隔定界符之间的值并将其放入变量中?

    System.out.print("Enter coordinates for two points as (x1, x2) (y1, y2): ");
    Scanner input = new Scanner (System.in);
    String wholeString = input.nextLine();
    Scanner stringScanner = new Scanner (wholeString).useDelimiter("[,\\s\\(\\)]*");
    x1 = stringScanner.nextDouble();
    x2 = stringScanner.nextDouble();
    y1 = stringScanner.nextDouble();
    y2 = stringScanner.nextDouble();
    slope = (y2 - y1) / (x2 - x1);

最佳答案

尝试使用此定界符:[,\\s\\(\\)]+

*表示0或更大

10-04 21:04