public static void main(String[] args) {
for(int x=0x>=0x++)
{
for(int y =0y>=0y++)
{
if((x*x+2*x*y+x==42)&&(y*y+2*x*y+x==42))
{
System.out.println(x+" "+y)
}
}
}
}
}
里面方程组随便设置,有些可能无解 就没答案
public class SquareEquation {
double a, b, c
public void setA(double a) {
this.a = a
}
public void setB(double b) {
this.b = b
}
public void setC(double c) {
this.c = c
}
public double getRootOne() {
double disk = calculateValidDisk()
return (-b + Math.sqrt(disk)) / (2 * a)
}
public double getRootTwo() {
double disk = calculateValidDisk()
return (-b - Math.sqrt(disk)) / (2 * a)
}
private double calculateValidDisk() {
if (a == 0) {
throw new EquationException("不是二次方程", EquationException.NONE_EQUATION)
}
double disk = b * b - 4 * a * c
if (disk <0) {
throw new EquationException("没有实根", EquationException.NO_REALROOT)
}
return disk
}
}
扩展资料
比如 方程是 x平方+3x+2=0;
public class Test
{
public static void main(String []args)
{
double a=1//二次项系数
double b=3//一次项系数
double c=2//常数项
double s=b*b-4*a*c
if(s<0)
{
System.out.print("b*b-4ac<0,方程无解!")
return
}
double ss=Math.sqrt(s)
double x1=(-1*b-ss)/(2*a)
double x2=(-1*b+ss)/(2*a)
System.out.println("x1="+x1)
System.out.println("x2="+x2)
}
}
参考资料:百度百科 Java
百度百科 一元二次方程
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)