日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/21 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java打印 a b c,创建一个java程序,按顺序给出3个术语作为输入(a,b,c)打印它们的根... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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 b2-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 b2-4ac *equals 0*, the program should print. “The equation has one X” where X is the only root

If b2-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

總結(jié)

以上是生活随笔為你收集整理的java打印 a b c,创建一个java程序,按顺序给出3个术语作为输入(a,b,c)打印它们的根...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。