java中来自同一行的多个输入

java中来自同一行的多个输入,java,Java,我的Java程序作业需要帮助。作业是使用java计算两点之间的距离。我完成了第一部分,如下所示: import java.util.Scanner; public class DistanceCalcEasy { public static void main(String[] args) { // Creating a new scanner object System.out.println("Distance Calculator"); Scanner in

我的Java程序作业需要帮助。作业是使用java计算两点之间的距离。我完成了第一部分,如下所示:

import java.util.Scanner;

public class DistanceCalcEasy
{
   public static void main(String[] args)
   {
   // Creating a new scanner object
   System.out.println("Distance Calculator");
   Scanner input = new Scanner(System.in);

   // Getting all of the coordinates
   System.out.print("Enter the X coordinate of the first point: ");
   double x1 = input.nextDouble();
   System.out.print("Enter the Y coordinate of the first point: ");
   double y1 = input.nextDouble();

   System.out.print("Enter the X coordinate of the second point: ");
   double x2 = input.nextDouble();
   System.out.print("Enter the Y coordinate of the second point: ");
   double y2 = input.nextDouble();

   // Calculating the distance between the points
   double distance = Math.sqrt( Math.pow((x2-x1),2) + Math.pow((y2-y1),2) );

   // Printing the distance to the User
   System.out.println("The distance between the points is " + distance);
   }
}
现在的问题是我需要再做一次同样的程序,但是“困难的方法”是允许用户输入一个坐标,比如1,2,而不是在他们自己的线上输入每个x和y。这是我经过一点研究后开始想到的:

import java.util.Scanner;

public class DistanceCalcHard
{
   public static void main(String[] args)
   {
      // Creating a new Scanner Object
      System.out.println("Distance Calculator");
      Scanner input = new Scanner(System.in);

      // Getting the data points
      System.out.print("Enter the first point x,y: ");
      String firstPoint = input.nextLine();

      System.out.print("Enter the second point x,y: ");
      String secondPoint = input.nextLine();

      Scanner scan = new Scanner(firstPoint).useDelimiter("\\s*,\\s*");
      while (scan.hasNextDouble() ) 
      { 

      }
      // Calculating the distance

      // Displaying the distance to the user
   }
}

这看起来是个好的开始吗?我想我可以做两个数组,每个点一个,然后用这种方法计算距离。有没有更简单的方法,或者有人能给我指出一个更好的方向?谢谢

像这样的东西怎么样:

import java.util.Scanner;

public class DistanceCalcEasy
{
   public static void main(String[] args)
   {
   // Creating a new scanner object
   System.out.println("Distance Calculator");
   Scanner input = new Scanner(System.in);

   // Getting all of the coordinates
   System.out.print("Enter the X,Y coordinate of the first point: ");
   String xy1in = input.nextLine();

   System.out.print("Enter the X,Y coordinate of the second point: ");
   String xy2in = input.nextLine();

   String[] xy1 = xy1in.split(",");
   String[] xy2 = xy2in.split(",");

   double x1 = Double.parseDouble(xy1[0]);
   double y1 = Double.parseDouble(xy1[1]);
   double x2 = Double.parseDouble(xy2[0]);
   double y2 = Double.parseDouble(xy2[1]);

   // Calculating the distance between the points
   double distance = Math.sqrt( Math.pow((x2-x1),2) + Math.pow((y2-y1),2) );

   // Printing the distance to the User
   System.out.println("The distance between the points is " + distance);
   }
}

将字符串拆分为两个值(即x,y->x和y)的一种更简单的方法是使用
string
对象的
split()
操作符

String[] pointA = firstPoint.split(",");
第二点也是如此。现在有了数组中的两点,
pointA[0]
是x值,
pointA[1]
是y值


有关该方法的更多文档,请参见

int[]point1=firstPoint.split(“,”)
给出数组中的点,例如
[1,2]