java编程 求解一元二次方程:ax^2+bx+c=0

编写一个程序,求解一元二次方程:ax^2+bx+c=0 。参数a,b及c从命令行输入。提示:需要用到 java.lang.Math 类, Math 类中提供了多种用于科学计算的方法,例如开方 sqrt, 求幂 pow等。 我的代码有些错误,不知道怎么改才能从命令行输入,请高手指点. public class Test2 { public static String x(int a,int b,int c){ int x = b*b-4*a*c; double d; if(x<0){ return "无解"; }else{ d = Math.pow(x, 0.5); return (-b-d)/(2*a)+":"+(-b+d)/(2*a); } } public static void main(String[] args) { String Str1,Str2,Str3; int m,n,q; m=(int)Str1; n=(int)Str2; q=(int)Str3; String c = Test2.x(m,n,q); System.out.println(c); } } 或者说有更好考虑得更全面的方法. 谢谢了.

可以用Scanner 逻辑上应该没错误 import java.util.Scanner; public class Test2 { public static String x(int a,int b,int c){ int x = b*b-4*a*c; double d; if(x<0){ return "无解"; }else{ d = Math.pow(x, 0.5); return (-b-d)/(2*a)+":"+(-b+d)/(2*a); } } public static void main(String[] args) { int m,n,q; Scanner s = new Scanner(System.in); m = s.nextInt(); n = s.nextInt(); q = s.nextInt(); String c = Test2.x(m,n,q); System.out.println(c); } }
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答