Java - wczytywanie współrzędnych wektorów

0

You have been given two 2D vectors. Find the angle (degrees) between them.

Input data format: The first line contains two components of the first vector, the second line contains two components of the second vector. Components in a line are separated by a space.

Output data format: One double value - an angle between two vectors. The result can have an error less than 1e-8.

Sample Input: 
1 3 
4 2 
Sample Output: 
45 

Napisałem kod jak poniżej, ale mam problem z wprowadzeniem w odpowiedni sposób współrzędnych wektorów. Bardzo proszę o pomoc.

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    String firstVectorComponents = scanner.nextLine();
    firstVectorComponents = (a + " " + b);
    System.out.println(firstVectorComponents);

    String secondVectorComponents = scanner.nextLine();
    secondVectorComponents = (c + " " + d);
    System.out.println(secondVectorComponents);

    double scalarProductOfVectors = (a * c) + (b * d);

    double X = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
    double Y = Math.sqrt(Math.pow(c, 2) + Math.pow(d, 2));

    double cos = scalarProductOfVectors / (X * Y);

    double angleInRadians = Math.asin(cos);
    double angleInDegrees = Math.toDegrees(angleInRadians);

    System.out.print(angleInDegrees);
  }
0

Najpierw zadeklaruj sobie te cztery zmienne do których będziesz chciał przypisać wartości.

double a = 0;
double b = 0;
double c = 0;
double d = 0;

Ciągnąc ten sposób który zacząłeś - ze Stringiem i wczytywaniem tego w jednej linii po spacji, możesz to zrobić w taki sposób:

 String firstVectorComponents = scanner.nextLine();
 String[] x = firstVectorComponents.split(" ");
    a = Double.valueOf(x[0]);
    b = Double.valueOf(x[1]);

Możesz też wczytywać każdą wartość osobno.

    c = scanner.nextDouble();
    d = scanner.nextDouble();

1 użytkowników online, w tym zalogowanych: 0, gości: 1