问题
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)");
}
}
}
回答1:
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
来源:https://stackoverflow.com/questions/16899771/creating-a-java-program-that-given-3-terms-as-inputa-b-c-in-that-order-prints