问题描述
Wrie方法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,其中平方根不是正数
- 如果b ^ 2 - 4ac是负数,则二次方程变为:( - b +/-√C)/2a
计算系数并打印出来(即X1是-b / 2a,Y1是sqrt(-C)/ 2i
注意:此问题不允许使用扫描仪
有人可以查看我的程序,并告诉我我哪里出错了,我只是删除我的扫描仪,使其成为没有扫描仪的程序?
import java.util.Scanner; // delet e $
公共类查找之后的这部分语句{
public static void main(String [] args)
{
}
public static double printRoots(){// should它在这里加倍还是int?
//读入系数a,b和c
扫描仪读卡器=新扫描仪(System.in);
int a = reader.nextInt();
System.out.println(输入值);
int b = reader.nextInt();
System.out.println(输入b的值);
int c = reader.nextInt();
System.out.println(输入c的值);
//现在compte the discriminimintat d
double discrimintant = d;
倍X,Y; //根1& root 2,分别是
//是步骤双X,Y必要吗?
double d =(b * b) - (4.0 * a * c);
if(d> 0.0){
d = Math.sqrt(d);
System.out.println(两个根是X和Y);
double X =( - b + d)/(2.0 * a); // X =根1,更大
double Y =( - b - d)/(2.0 * a); // Y = root 2,它是较小的根
System.out.println(Root 1= X和Root 2=Y);
}
else {
if(d == 0.0)//then ...怎么写?
System.out.println(公式有一个根X)//其中X是唯一的根
double X =( - b + 0.0)/(2.0 * a); //重复root
System.out.println(Root=X);
}
else {
if(d< 0.0)
System.out.println(等式有两个根(-X1 + Y1i)和(-X2 + Y2i) ));
//其中i代表负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)和Root 2=( - X2 + Y2i));
}
}
}
你可以传递输入从命令行。你将获得args数组中的数据
in public static void main(String [] args)
这里args是指使用
<$运行java程序c $ c> java MyApp arg1 arg2
主要 args [0]中的
是
arg1
和 args [1]
是 arg2
所以在您的情况下运行应用程序,如下面的命令
java findingRoots 1 2 3
and main
int a = Integer.parseInt(args [0])
NB 我想你想验证命令行参数检查args.length以及它们是否为int
Wrie a method printRoots that given 3 terms as input(a,b,c) in that order prints their roots
We have the following given information
If b²-4ac is a positive number, your program should print "The two roots are X and Y" where X is the larger root and Y is the smaller root
If b²-4ac *equals 0*, the program should print. "The equation has one X" where X is the only root
If b²-4ac is a negative number, the program should print." The equation has two roots(-X1 + Y1i) and (-X2 and Y2i)
The term can be determined based on:
- If b^2 - 4ac is a negative number, then the quadratic equation becomes: (-b+/- √C)/2a-This means the equation can be simplified to become (-b+/- √Ci)/2a where the square root is not a positive number
Calculate the coefficient and print that(i.e X1 is -b/2a and Y1 is sqrt(-C)/2i
Note: Not allowed to use Scanners for this question
Is it possible for someone to review my program and tell me where I have gone wrong and do i just remove my scanners to make it a program without scanners?
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)");
}
}
}
you can pass input from command lines. You will get the data at args array
in public static void main(String[] args)
here args refers to command line arguements
when you run a java program using
java MyApp arg1 arg2
in your main args[0]
is arg1
and args[1]
is arg2
So in your case run the app like following command
java findingRoots 1 2 3
and in main
int a= Integer.parseInt(args[0])
N.B I think you would like to validate the command line parameters. check both the args.length and if they are int or not
这篇关于创建一个java程序,按顺序给出3个术语作为输入(a,b,c)打印它们的根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!