當我嘗試在程序中獲取矢量的方向時,我的if語句無法正常工作。如果語句沒有按照我想要的那樣做
問題是,當我輸入的< -1向量,1>
我得到45°而不是125°
源代碼
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String[] args)
{
//Decloration
Scanner in = new Scanner(System.in);
int rty;
double v1;
double v2;
double dir;
double mag;
double V1;
double V2;
do {
//Input
System.out.println("Please input x");
v1 = in.nextDouble();
System.out.println("Please input y");
v2 = in.nextDouble();
//Calculating
dir = Math.toDegrees(Math.atan(v2/v1));
if (v1 >= 0) {
if (v2 < 0) {
dir+=270;
}
} else {
if (v2 < 0) {
dir+=180;
} else {
dir+=90;
}
}
mag = Math.sqrt((v1*v1)+(v2*v2));
V1 = v1/mag;
V2 = v2/mag;
//Output
System.out.println("The unit vector is <" + V1 +", " + V2 + ">");
System.out.println("The magnitude is " + mag);
System.out.println("The direction is " + dir);
System.out.println("\n\nRty?");
//Quit
rty = in.nextInt();
System.out.println("\n");
dir = 0;
} while (rty == 1);
}
}
有誰知道什麼是錯?
首先,你可能想'數學。 atan2(v2,v1)'。 –
使用調試器。 – redFIVE