java打印 a b c,创建一个java程序,按顺序给出3个术语作为输入(a,b,c)打印它们的根...

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值