制作一个打印二次方程根的java代码

制作一个打印二次方程根的java代码,java,computer-science,Java,Computer Science,我以前曾发布过这个问题。但是我对它做了大量的编辑。由于我的java代码没有编译,我想请求帮助纠正我的步骤 编写一个方法printRoots,将3个术语作为输入,按顺序表示a、b和c,并打印它们的根 我们有以下给定的信息 **如果b²-4ac是正数,您的程序应该打印“两个根是X和Y”,其中X是较大的根,Y是较小的根 如果b²-4ac等于0,程序应打印。“方程有一个X”,其中X是唯一的根 如果b²-4ac是负数,程序应该打印。“方程有两个根(-X1+Y1i)和(-X2和Y2i)** 该术语可根据以下

我以前曾发布过这个问题。但是我对它做了大量的编辑。由于我的java代码没有编译,我想请求帮助纠正我的步骤

编写一个方法printRoots,将3个术语作为输入,按顺序表示a、b和c,并打印它们的根

我们有以下给定的信息

**如果b²-4ac是正数,您的程序应该打印“两个根是X和Y”,其中X是较大的根,Y是较小的根

如果b²-4ac等于0,程序应打印。“方程有一个X”,其中X是唯一的根

如果b²-4ac是负数,程序应该打印。“方程有两个根(-X1+Y1i)和(-X2和Y2i)**

该术语可根据以下内容确定:

如果b^2-4ac为负数,则二次方程变为:(-b+/-√C) /2a-这意味着方程可以简化为(-b+/-√Ci)/2a,其中平方根不是正数 计算系数并打印(即X1为-b/2a,Y1为sqrt(-C)/2i)

注意:此问题不允许使用扫描仪

是否有人可以查看我的程序,并告诉我哪里出了问题,我是否只是删除了扫描仪,使其成为一个没有扫描仪的程序?那么如何输入a、b、c??

请注意:您不应在此方法中使用扫描仪。要测试方法,可以在主方法中使用SCanner,也可以从主方法将值硬编码到代码中,然后使用这些输入或输出调用方法printRoots


import java.util.Scanner//删除后的此部分
公共类查找根{
公共静态void main(字符串[]args)
{
}
publicstaticdoubleprintroots(){//这里应该是double还是int?
//读入系数a、b和c
扫描仪阅读器=新扫描仪(System.in);
System.out.println(“输入a的值”);
int a=reader.nextInt();
System.out.println(“输入b的值”);
int b=reader.nextInt();
System.out.println(“输入c的值”);
int c=reader.nextInt();
//现在,完成这项辩论
双判别式=(数学功率(b,2.0))-(4*a*c);
d=数学sqrt(判别式);
double X,Y;//分别为根1和根2
//是否需要双X,Y步?
双d=(b*b)-(4.0*a*c);
如果(判别式>0.0){
X=(-b+d)/(2.0*a);//X=根1,较大者
**//我是在X前面加int还是double?**
Y=(-b-d)/(2.0*a);//Y=根2,它是较小的根
字符串root1=Double.toString(X)
字符串root2=Double.toString(Y)
System.out.println(“两个根是X和Y:“+root1+”和“+root2”);
}
否则{
如果(判别式==0.0)
X=(-b+0.0)/(2.0*a);//重复根
字符串root2=Double.toString(X)
System.out.println(“方程有一个根X:+root1);//其中X是唯一的根
}
if(判别式<0.0){
X1=-b/(2*a);
Y1=(数学sqrt(-C))/(2*a);
X2=-b/(2*a);
Y2=((数学量-C))/(2*a);
字符串root1=Double.toString(X1)
字符串root2=Double.toString(Y1)
字符串root3=Double.toString(X2)
字符串root4=Double.toString(Y2)
println(“方程有两个根:”+(root1+(root2)“i”)和“+(root3+(root4)”i”);
//其中i表示负1的平方根
}
}
}

好的,所以代码有点错误。这是您的程序的运行版本。(或者至少我能尽可能接近您要做的事情)

//导入java.util.Scanner;//之后删除此部分
公共类查找根{
公共静态void main(字符串[]args){
double-temp=printRoots(Integer.parseInt(args[0])、Integer.parseInt(args[1])、Integer.parseInt(args[2]);
}
公共静态双打印根(inta,intb,intc){//这里应该是double还是int?
//读入系数a、b和c
//现在,完成这项辩论
双判别式=(数学功率(b,2.0))-(4*a*c);
双d=数学sqrt(判别式);
double X=0,Y=0;//分别为根1和根2
//是否需要双X,Y步?
d=(b*b)-(4.0*a*c);
如果(判别式>0.0){
X=(-b+d)/(2.0*a);//X=根1,较大者
//我是在X前面加int还是double?**
Y=(-b-d)/(2.0*a);//Y=根2,它是较小的根
字符串root1=Double.toString(X);
字符串root2=Double.toString(Y);
System.out.println(“两个根是X和Y:“+root1+”和“+root2”);
}
else if(判别式==0.0){
X=(-b+0.0)/(2.0*a);//重复根
字符串root2=Double.toString(X);
System.out.println(“方程有一个根X:+root2);//其中X是唯一的根
}
else if(判别式<0.0){
双X1=-b/(2*a);
双Y1=(数学sqrt(-c))/(2*a);
双X2=-b/(2*a);
双Y2=((数学sqrt(-c))/(2*a);
字符串root1=Double.toString(X1);
字符串root2=Double.toString(Y1);
字符串root3=Double.toString(X2);
字符串root4=Double.toString(Y2);
System.out.println(“方程有两个根:“+root1+root2+”和“+root3+root4”);
//其中i表示负1的平方根
}
返回-1;
}
}
要运行此代码,只需键入:
java查找根1 2 3
其中1 2 3分别是a、b、c值


这不是您想要的工作方式(我假设)。这应该可以让您开始解决问题,因为它现在可以运行。让我们看看您可以从这里做些什么。

也许前面的
**
import java.util.Scanner;//delete this part after 
public class findingRoots {
  public static void main(String[] args)
    {
  }
    public static double printRoots (){ //should it be double here or int? 
      //read in the coefficients a,b,and c 
    Scanner reader = new Scanner(System.in);
    System.out.println("Enter the value of a");
    int a=reader.nextInt();
    System.out.println("Enter the value of b");
    int b=reader.nextInt();
    System.out.println("Enter the value of c");
    int c=reader.nextInt();        
    //now compte the discrimintat d 
     double discriminant = (Math.pow(b, 2.0)) - (4 * a * c);
     d=Math.sqrt(discriminant);
     double X,Y; //root 1 & root 2, respectively
     // is the step double X,Y necessary? 
     double d = (b*b)-(4.0*a*c);

       if (discriminant > 0.0){ 
       X = (-b + d)/(2.0 * a ); //X= root 1, which is larger 
       **//do i put int or double in front of X?** 
       Y = (-b - d)/(2.0 *a); //Y= root 2, which is the smaller root 
           String root1 = Double.toString(X)
       String root2 = Double.toString(Y)
   System.out.println("The two roots are X and Y:" + root1 + "and" + root2);
 }
 else{
   if (discriminant==0.0) 
     X = (-b + 0.0)/(2.0 * a);//repeated root
     String root2 = Double.toString(X)
     System.out.println("The equation has one root X:"  + root1);//where X is the only root 
 }
 if(discriminant < 0.0){
    X1 = -b/(2*a);
    Y1 = (Math.sqrt(-C))/(2*a);
    X2 = -b/(2*a);
    Y2 = (-(Math.sqrt(-C)))/(2*a);
    String root1 = Double.toString(X1)
    String root2 = Double.toString(Y1)
    String root3 = Double.toString(X2)
    String root4 = Double.toString(Y2)
    System.out.println("The equation has two roots:" + (root1 + (root2)"i") "and" + (root3 + (root4)"i");
   // where i represents the square root of negative 1 
 }
}
}
//import java.util.Scanner;//delete this part after 
public class findingRoots {
    public static void main(String[] args) { 
        double temp = printRoots(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]));
    }
    public static double printRoots (int a, int b, int c){ //should it be double here or int? 
       //read in the coefficients a,b,and c 
       //now compte the discrimintat d 
       double discriminant = (Math.pow(b, 2.0)) - (4 * a * c);
       double d=Math.sqrt(discriminant);
       double X = 0,Y = 0; //root 1 & root 2, respectively
       // is the step double X,Y necessary? 
       d = (b*b)-(4.0*a*c);

       if (discriminant > 0.0) { 
           X = (-b + d)/(2.0 * a ); //X= root 1, which is larger 
           //do i put int or double in front of X?** 
           Y = (-b - d)/(2.0 *a); //Y= root 2, which is the smaller root 
           String root1 = Double.toString(X);
           String root2 = Double.toString(Y);
           System.out.println("The two roots are X and Y:" + root1 + "and" + root2);
       }
       else if (discriminant==0.0){
           X = (-b + 0.0)/(2.0 * a);//repeated root
           String root2 = Double.toString(X);
           System.out.println("The equation has one root X:"  + root2);//where X is the only root 
       }
       else if (discriminant < 0.0){
           double X1 = -b/(2*a);
           double Y1 = (Math.sqrt(-c))/(2*a);
           double X2 = -b/(2*a);
           double Y2 = (-(Math.sqrt(-c)))/(2*a);
           String root1 = Double.toString(X1);
           String root2 = Double.toString(Y1);
           String root3 = Double.toString(X2);
           String root4 = Double.toString(Y2);
           System.out.println("The equation has two roots:" + root1 + root2  + "and" + root3 + root4);
           // where i represents the square root of negative 1 
      }
      return -1;
    }
}