2015-09-30 126 views
3

所以我是新來的Java和我有一個任務要做的類,但我卡住了。班級應該使用二次方程來找出兩條線的交點。我被告知有特定的輸入類,所以d = 5,f = -3,g = 2,m = 1和b = 3,兩個交點應該是(1,4)和(-.20, 2.8)。我遇到的問題是輸出返回(NaN,NaN)和(NaN,NaN)而不是正確答案。我的代碼有什麼問題讓我得到這個答案?爲什麼輸出(Nan,Nan)?

public class Intersect{ 
public static void main(String args[]){ 

    //prompt user for parabola vars d f g 
    System.out.println("Enter the constant d:"); 
    int d = IO.readInt(); 
    System.out.println("Enter the constant f:"); 
    int f = IO.readInt(); 
    System.out.println("Enter the constant g:"); 
    int g = IO.readInt(); 

    // y = dx^2 + fx + g 

    //promt user for line vars m b 
    System.out.println("Enter the constant m:"); 
    int m = IO.readInt(); 
    System.out.println("Enter the constant b:"); 
    int b = IO.readInt(); 

    // y = mx + b 

    //compute intersection 
    // dx^2 + fx + g = mx + b 
    // x^2 * (d) + x * (f-m) + (g-b) = 0 

    int a = d; 
    int z = (f-m); 
    int c = (g-b); 

    double x1 = -z + (Math.sqrt (z^2 - 4 * a * c)/(2 * a)); 
    double x2 = -z - (Math.sqrt (z^2 - 4 * a * c)/(2 * a)); 
    double y1 = m * x1 + b; 
    double y2 = m * x2 - b; 

    //output each intersection on its own line, System.out.println() is ok for this answer submission 
    System.out.println("The intersection(s) are:"); 
    System.out.println("(" + x1 + "," + y1 + ")"); 
    System.out.println("(" + x2 + "," + y2 + ")"); 
} 
} 
+2

我很困惑。輸出「(4.42,7.42)和(3.57,.57)」,還是輸出「(南,南)」? – Kevin

+0

哦等,輸出結果是(南,南) –

+3

'^'不是Java中的指數運算符。 'z^2'不是你想象的那樣。 – azurefrog

回答

4

^ is the xor operator in java and not the exponentiation operator。因此,表示方式z^2 - 4 * a * c計算出的結果是否定的。

從您提供的輸入中,z = -4, a = 5, c = -1。該表達式翻譯爲-4^2 - 4 * 5 * -1。請注意,*+具有higher precedence than ^,即評估順序是(-4^(2 - ((4 * 5) * -1))) = -22

然後你試圖找到-22的平方根,其根據Math.sqrt()NaN

使用Math.pow(z, 2),或者乾脆使用z * z代替:

所有的
Math.sqrt(z * z - 4 * a * c); // Note: This time operator precedence works, 
           // But you should use parentheses wherever 
           // the expression seems ambiguous. 
1

首先^不是冪運算符,是什麼原因導致楠是你負參數Math.sqrt傳遞的事實。

從Java引用(http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html):

public static double sqrt(double a) 
Returns the correctly rounded positive square root of a double value.  Special cases: 
If the argument is NaN or less than zero, then the result is NaN. 
If the argument is positive infinity, then the result is positive infinity. 
If the argument is positive zero or negative zero, then the result is the same as the argument. 
Otherwise, the result is the double value closest to the true mathematical square root of the argument value. 
Parameters: 
a - a value. 
Returns: 
the positive square root of a. If the argument is NaN or less than zero, the result is NaN. 
0

,是你的操作順序是導致你得到NaN的結果。 試試這個(爲了方便添加變量):

int a = d; 
int z = f - m; 
int negZ = -z; 
int c = g - b; 

double sq = Math.sqrt((z * z) - (4 * a * c)); 
double a2 = 2 * a; 
double x1 = (negZ + sq)/a2; 
double x2 = (negZ - sq)/a2; 
double y1 = (m * x1) + b; 
double y2 = (m * x2) - b;