我之前发过这个问题。但是对它进行了大量的编辑。并且我想请求帮助来纠正我的步骤,因为我的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。要测试您的方法,您可以在main方法中使用SCanner,或者您可以从main方法将值硬编码到代码中,然后使用这些输入或输出调用printRoots方法。
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
}
}
}
答案 0 :(得分:0)
好的,所以代码有点不对劲。这是您的程序的运行版本。 (或者至少尽可能接近你想要做的事情)。
//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;
}
}
要运行此代码,只需键入:
java findingRoots 1 2 3
其中1 2 3分别是你的a,b,c值。
这不按你想要的方式工作(我假设)。这应该可以帮助您解决问题,因为它现在可以运行。让我们看看你能从这里做些什么。