2012-10-20 54 views
1

有人可以看看我的代碼的二次方程嗎?我總是有根-2錯誤(中「root1的原始型雙不具有場根-2)。我只需要打印出的兩個根,謝謝。有人可以看看我的代碼的二次方程(在Java中)?


public class QuadraticEqn { 
public static void main(String[] args) { 
    System.out.print(quadratic(-7, 4, 3)); 
} 
    public static double quadratic(int a, int b, int c){ 
     double discriminant = (b*b)-4*a*c; 
     double root1 = -1*b + Math.sqrt(discriminant); 
     double root2 = -1*b - Math.sqrt(discriminant); 
     return (root1, root2); 
} 
} 

回答

3

Java可以不返回記錄的那樣,可能比較容易,因爲它們是相同類型的返回數組。

    public static double[] quadratic(int a, int b, int c){ 
        double discriminant = (b*b)-4*a*c; 
        double root1 = -1*b + Math.sqrt(discriminant); 
        double root2 = -1*b - Math.sqrt(discriminant); 
     double[] array = {root1, root2}; 
        return array; 
    } 
3

不可能回到這樣兩個雙值。嘗試返回雙排值的數組。

public class QuadraticEqn { 
     public static void main(String[] args) { 
      double[] root = quadratic(-7, 4, 3); 
      System.out.print(root[0] + " " + root[1]); 
     } 

     public static double[] quadratic(int a, int b, int c) { 
      double discriminant = (b * b) - 4 * a * c; 
      double[] root = new double[2]; 
      root[0] = -1 * b + Math.sqrt(discriminant); 
      root[1] = -1 * b - Math.sqrt(discriminant); 
      return root; 
     } 
    } 
-2

不知道你試圖完成什麼,我做了一個類似的程序使用JavaScript,並繼承了代碼,它非常簡單。順便說一句,二次方程是-b±√D/ 2a,我不認爲你完全把它。繼承人我寫的代碼: 另外,我認爲問題出在你已經退回的部分:(root1,root2),使它成爲一個數組或只做2個返回命令。 即回報(目錄root1) 回報(根-2)不知道你是否中央社有2返回插件的功能,而不是一個Java程序員,很抱歉,但我想:d

else if(choice == 2){//quadratic start 
    alert("the equation is of the form : ax^2 + bx + c == 0 , only input the coefficients i.e - the value of ax^2 is a, or the value of bx is b, not bx. The value of b for the equation 5x^2 + 7x +3 is 7, not 7x"); 
    var a = prompt("Put in the value of a");//declaring variables 
    var b = prompt("Put in the value of b, if the bx part of the equation doesn't exist, input 0. Ex for equation 2x^+6==0 , b ==0, since its technically 2x^2 + 0b + 6 == 0"); 
    var c = prompt("Put in the value of c, if the c part of the equation doesn't exist, input 0. Ex for equation 2x^+6x==0 , c ==0, since its technically 2x^2 + 6b + 0 == 0"); 
    var D = ((b*b)-(4*a*c));//computing discriminant 
    if(D < 0){ 
    alert("The quadratic equation doesn't have real roots; the closest value is : " + (-b/2) +" + i/2"); 
    } 
    else{ 
    root1 = (- b + Math.sqrt(D))/(2*a); 
    root2 = (- b - Math.sqrt(D))/(2*a); 
    } 
    if(D===0){ 
    console.log("Both roots are equal, their value is " + root1); 
    alert("Both roots are equal, their value is " + root1); 
    } 
    else if (D > 0){ 
    console.log("The roots of the equation are: " + root1 + " and " + root2); 
    alert("The roots of the equation are: " + root1 + " and " + root2); 
    } 
    }//quadratic end 

和繼承人的完整的程序:

<!DOCTYPE html> 
<head> 
<script type="text/javascript" src ="code.js"></script> 
<script type="text/javascript"> 
var main = function(){//Linear in 2 start 
var choice = prompt("Choose your type of equation : Type 1 for linear in 2 variables, 2 for quadratic in one variable "); 
if(choice ==1){ 
alert("The two equations are of the forms a1x + b1y + c1 = 0 and a2x + b2y + c2 = 0 respectively") 
var a1 = prompt("Enter value of a (a1) for equation 1"); 
var b1 = prompt("Enter value of b (b1) for equation 1"); 
var c1 = prompt("Enter value of c (c1) for equation 1"); 
var a2 = prompt("Enter value of a (a2) for equation 2"); 
var b2 = prompt("Enter value of b (b2) for equation 2"); 
var c2 = prompt("Enter value of c (c2) for equation 2"); 
if(a1/a2===b1/b2===c1/c2){ 
alert("No solution is possible for the equation, i.e. the lines are parallel") 
} 
else if(a1/a2===b1/b2 && b1/b2!=c1/c2){ 
alert("The lines are collinear and the values of x and y are infinite") 
} 
else{ 
var ansy = (c2*a1-c1*a2)/(b1*a2-b2*a1); 
var ansx = (c1-b1*ansy/a1); 
alert("x is equal to : " + ansx); 
alert("y is equal to : " + ansy); 
} 
} 
else if(choice == 2){//quadratic start 
alert("the equation is of the form : ax^2 + bx + c == 0 , only input the coefficients i.e - the value of ax^2 is a, or the value of bx is b, not bx. The value of b for the equation 5x^2 + 7x +3 is 7, not 7x"); 
var a = prompt("Put in the value of a");//declaring variables 
var b = prompt("Put in the value of b, if the bx part of the equation doesn't exist, input 0. Ex for equation 2x^+6==0 , b ==0, since its technically 2x^2 + 0b + 6 == 0"); 
var c = prompt("Put in the value of c, if the c part of the equation doesn't exist, input 0. Ex for equation 2x^+6x==0 , c ==0, since its technically 2x^2 + 6b + 0 == 0"); 
var D = ((b*b)-(4*a*c));//computing discriminant 
if(D < 0){ 
alert("The quadratic equation doesn't have real roots; the closest value is : " + (-b/2) +" + i/2"); 
} 
else{ 
root1 = (- b + Math.sqrt(D))/(2*a); 
root2 = (- b - Math.sqrt(D))/(2*a); 
} 
if(D===0){ 
console.log("Both roots are equal, their value is " + root1); 
alert("Both roots are equal, their value is " + root1); 
} 
else if (D > 0){ 
console.log("The roots of the equation are: " + root1 + " and " + root2); 
alert("The roots of the equation are: " + root1 + " and " + root2); 
} 
}//quadratic end 
} 
var again = confirm("Proceed to equation selection menu?"); 
if(again === true){ 
main(); 
} 
</script> 
<title>Equation Solver</title> 
</head> 
<body> 
</body> 
</html> 
相關問題