2012-10-20 3 views
1

누군가 제 코드를 2 차 방정식으로 살펴볼 수 있습니까? 난 항상 ROOT2에 오류 ("ROOT1의 원시 형 더블 필드 ROOT2이없는)을 얻을. 난 그냥 두 뿌리를 인쇄해야합니다. 감사합니다.누군가 제 코드를 자바에서 2 차 방정식으로 살펴볼 수 있습니까?


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

자바는 할 수 없습니다 그들은 동일한 유형에 있기 때문에 아마 가장 쉬운 배열을 반환하는, 그런 튜플을 반환합니다.

    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

을 두 개의 더블 값이 방법을 반환 할 수 없습니다. duble 값의 배열을 반환하려고합니다.

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; 
     } 
    } 
5

여전히 2a으로 나눌 필요가 있습니다.

-2

개봉하려는 사용자가 무엇인지 잘 모르겠습니다. 자바 스크립트를 사용하여 비슷한 프로그램을 만들었으며 코드가 매우 단순합니다. btw, 2 차 방정식은 -b ± √D/2a입니다. heres 내가 작성한 코드를 heres : 또한, 문제는 당신이 반환 된 부분에 있습니다 : (root1, root2), 배열로 만들거나 그냥 2 반환 명령을 확인하십시오. 즉 반환 당신이이 반환을 CNA의 경우하지 않도록 (ROOT1) 리턴 (ROOT2는) 함수가 아닌 자바 프로그래머, 미안 인,하지만 난 시도 : 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 

및 heres는 전체 프로그램 :

<!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>