java 先定义类 求一元二次方程的解

问题描述:

java 先定义类 求一元二次方程的解

import java.util.*;
public class Hello {
private static Scanner san = new Scanner(System.in);
public static double readNum(String info) {
System.out.print(info);
double d = san.nextDouble();
return d;
}
public static void main(String[] args) {
double a = Hello.readNum("输入平方项系数:");
double b = Hello.readNum("输入一次项系数:");
double c = Hello.readNum("输入常数项:");
System.out.println(a +"x^2 + " + b + "x + " + c + " = 0");
double d = b * b - 4 * a * c;
if(d System.out.println("无解!");
} else if(d == 0) {
double r = - b / 2 * a;
System.out.printf("x1=x2=%2.1f",r);
} else {
double r1 = (-b + Math.sqrt(d)) / 2 * a;
double r2 = (-b - Math.sqrt(d)) / 2 * a;
System.out.printf("x1=%2.1f,x2=%2.1f",r1,r2);
}
}
}