我在java中做了一个简单的测试,(我是初学者)我试图制作一个程序来计算二度方程的区别并显示结果,但是当我改变了nom1和nom2的标志,该程序没有显示任何结果,我已经做了一些测试,我确信问题是存在的,但我不知道如何解决它。 欢迎任何帮助。
这是我的代码:
public class test {
public static void main(String[] args) {
double a = 2;
double b = 5;
double c = 3;
double delta = b*b - 4 * a * c;
double den = 2 * a;
if(delta == 0){
double nom = b;
double pgcd = pgcd(nom, den);
double x0nom = nom/pgcd;
double x0den = den/pgcd;
System.out.println("x0 = "+String.valueOf(x0nom)+" / "+String.valueOf(x0den));
}else if(delta > 0){
// x1:
double nom1 = -b + Math.sqrt(delta);
double pgcd1 = pgcd(nom1, den);
int x1nom = (int) (nom1 / pgcd1);
int x1den = (int) (den / pgcd1);
// x2:
double nom2 = -b - Math.sqrt(delta);
double pgcd2 = pgcd(nom2, den);
int x2nom = (int) (nom2 / pgcd2);
int x2den = (int) (den / pgcd2);
System.out.println("pgcd = "+String.valueOf(pgcd1)+" "+String.valueOf(pgcd2));
System.out.println("x1 = "+String.valueOf(x1nom)+" / "+String.valueOf(x1den));
System.out.println("x2 = "+String.valueOf(x2nom)+" / "+String.valueOf(x2den));
}else if(delta < 0){
}
}
public static double pgcd(double a, double b) {
while (a != b) {
if (a < b){
b = b - a;
}else{
a = a - b;
}
}
return a;
}
}
答案 0 :(得分:1)
public static double pgcd(double a, double b) {
return (BigInteger.valueOf((long) a).gcd(BigInteger.valueOf((long) b)).intValue());
}