有人能看一下我的代码中的二次方程(java)吗?

有人能看一下我的代码中的二次方程(java)吗?,java,quadratic,Java,Quadratic,有人能看一下我的二次方程代码吗?我总是在root2上出错(“root1的基本类型double没有root2字段)。我只需要打印出这两个根。谢谢 Java不能像那样返回元组,这可能是返回数组的最简单方法,因为它们是相同的类型     public static double[] quadratic(int a, int b, int c){         double discriminant = (b*b)-4*a*c;         double root1 = -1*b + Math.

有人能看一下我的二次方程代码吗?我总是在root2上出错(“root1的基本类型double没有root2字段)。我只需要打印出这两个根。谢谢



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

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

无法以这种方式返回两个双精度值。请尝试返回双精度值数组

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

无法以这种方式返回两个双精度值。请尝试返回双精度值数组

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

你仍然需要除以
2a

你仍然需要除以
2a

不确定你想要完成什么,我用javascript编写了一个类似的程序,代码如下,非常简单。顺便说一句,二次方程是-b”√D/2a,我认为你没有把它完全写进去。这是我写的代码: 此外,我认为问题在于返回的部分:(root1,root2),将其放入数组中,或者只执行2个返回命令。 即返回(root1) return(root2)不确定函数中是否有2个returns,不是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
else如果(选项==2){//二次启动
警告(“方程式的形式为:ax^2+bx+c==0,仅输入系数,即——ax^2的值为a,或bx的值为b,而非bx。方程式5x^2+7x+3的b值为7,而非7x”);
var a=prompt(“输入a的值”);//声明变量
var b=prompt(“输入b的值,如果方程的bx部分不存在,则为方程2x^+6==0输入0.Ex,b==0,因为从技术上讲它是2x^2+0b+6==0”);
var c=prompt(“输入c的值,如果方程的c部分不存在,则为方程2x^+6x==0输入0.Ex,c==0,因为从技术上讲它是2x^2+6b+0==0”);
var D=((b*b)-(4*a*c));//计算判别式
if(D<0){
警告(“二次方程没有实根;最近的值是:”+(-b/2)+“+i/2”);
}
否则{
root1=(-b+数学sqrt(D))/(2*a);
root2=(-b-数学sqrt(D))/(2*a);
}
如果(D==0){
log(“两个根相等,其值为“+root1”);
警报(“两个根相等,其值为“+root1”);
}
否则,如果(D>0){
log(“方程的根是:“+root1+”和“+root2”);
警报(“方程式的根为:“+root1+”和“+root2”);
}
}//二次端
以下是完整的程序:

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

var main=function(){//Linear in 2 start
var choice=prompt(“选择您的方程类型:1型表示2个变量中的线性,2型表示一个变量中的二次”);
如果(选项==1){
警报(“两个方程式的形式分别为a1x+b1y+c1=0和a2x+b2y+c2=0”)
var a1=提示(“为方程式1输入a(a1)的值”);
var b1=提示(“为方程式1输入b(b1)的值”);
var c1=提示(“为方程式1输入c(c1)的值”);
var a2=提示(“为方程式2输入a(a2)的值”);
var b2=提示(“为方程式2输入b(b2)的值”);
var c2=提示(“为方程式2输入c(c2)的值”);
如果(a1/a2==b1/b2==c1/c2){
警报(“方程不可能求解,即直线平行”)
}
否则如果(a1/a2==b1/b2&&b1/b2!=c1/c2){
警报(“线是共线的,x和y的值是无限的”)
}
否则{
变量ansy=(c2*a1-c1*a2)/(b1*a2-b2*a1);
var ansx=(c1-b1*ansy/a1);
警报(“x等于:”+ansx);
警报(“y等于:”+ansy);
}
}
如果(选项==2){//则为else
警告(“方程式的形式为:ax^2+bx+c==0,仅输入系数,即——ax^2的值为a,或bx的值为b,而非bx。方程式5x^2+7x+3的b值为7,而非7x”);
var a=prompt(“输入a的值”);//声明变量
var b=prompt(“输入b的值,如果方程的bx部分不存在,则为方程2x^+6==0输入0.Ex,b==0,因为从技术上讲它是2x^2+0b+6==0”);
var c=prompt(“输入c的值,如果方程的c部分不存在,则为方程2x^+6x==0输入0.Ex,c==0,因为从技术上讲它是2x^2+6b+0==0”);
var D=((b*b)-(4*a*c));//计算判别式
if(D<0){
警告(“二次方程没有实根;最近的值是:”+(-b/2)+“+i/2”);
}
否则{
root1=(-b+数学sqrt(D))/(2*a);
root2=(-b-数学sqrt(D))/(2*a);
}
如果(D==0){
log(“两个根相等,其值为“+root1”);
警报(“两个根相等,其值为“+root1”);
}
否则,如果(D>0){
log(“方程的根是:“+root1+”和“+root2”);
警报(“方程式的根为:“+root1+”和“+root2”);
}
}//二次端
}
var再次=确认(“进入方程式选择菜单?”);
如果(再次===真){
main();
}
方程求解器

我不确定你想要实现什么,我用javascript编写了一个类似的程序,代码如下,非常简单。顺便说一句,二次方程是-b±√D/2a,我认为你没有把它完全写进去。这是我写的代码: 此外,我认为问题在于返回的部分:(root1,root2),将其放入数组中,或者只执行2个返回命令。 即返回(root1) return(root2)不确定函数中是否有2个returns,不是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
else如果(选项==2){//二次启动
警告(“方程式的形式为:ax^2+bx+c==0,仅输入系数,即——ax^2的值为a,或bx的值为b,而非bx。方程式5x^2+7x+3的b值为7,而非7x”);
var a=prompt(“输入a的值”);//声明变量
var b=提示符(“如果方程的bx部分不存在,输入b的值,inpu