写一个方法 printRoots,它给出 3 个项作为 input(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
注意:这个问题不允许使用扫描仪
有人可以查看我的程序并告诉我哪里出了问题,我是否可以删除我的扫描仪以使其成为没有扫描仪的程序?
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);
int a=reader.nextInt();
System.out.println("Enter the value of a");
int b=reader.nextInt();
System.out.println("Enter the value of b");
int c=reader.nextInt();
System.out.println("Enter the value of c");
//now compte the discrimintat d
double discrimintant = d;
double X,Y; //root 1 & root 2, respectively
// is the step double X,Y necessary?
double d = (b*b)-(4.0*a*c);
if (d > 0.0){
d = Math.sqrt(d);
System.out.println("The two roots are X and Y");
double X = (-b + d)/(2.0 * a ); //X= root 1, which is larger
double Y = (-b - d)/(2.0 *a); //Y= root 2, which is the smaller root
System.out.println("Root 1" = X "and" "Root 2" "=" Y);
}
else{
if (d==0.0) //then...how to write?
System.out.println("The equation has one root X")//where X is the only root
double X = (-b + 0.0)/(2.0 * a);//repeated root
System.out.println("Root" "=" X);
}
else{
if(d < 0.0)
System.out.println("The equation has two roots (-X1 + Y1i) and (-X2 +Y2i)");
// where i represents the square root of negative 1
double X1 = -b/(2*a);
double Y1 = (Math.sqrt(-C))/(2*a);
double X2 = -b/(2*a);
double Y2 = (-(Math.sqrt(-C)))/(2*a);
double Y2 = (-(Math.sqrt(-C)))/(2*a);
System.out.println("Root 1" "=" (-X1 + Y1i) "and" "Root 2" "=" (-X2 +Y2i)");
}
}
}