You need to enable JavaScript to run this app. 求助:克莱姆法则Java实现中getDeterminant()莫名返回0
优惠活动
大模型
产品
解决方案
定价
更多

求助:克莱姆法则Java实现中getDeterminant()莫名返回0

克莱姆法则实现中的行列式计算问题

我在实现3变量线性方程组的克莱姆法则时遇到问题:getDeterminant()方法在本不应返回0的场景下总是返回0(并非持续出现),将数据类型从double替换为float后问题依旧。以下是我的完整代码:

import java.util.Scanner;
class CramersRule{
    //Numbers for the 3-Variable Linear Equation.
    private double a1, a2, a3;
    private double b1, b2, b3;
    private double c1, c2, c3;
    private double d1, d2, d3;
    
    CramersRule(){
        
    }
    //Sets the 1st Linear Equation.
    public void setLinearEquation1 (double a1, double b1, double c1, double d1){
        this.a1 = a1;
        this.b1 = b1;
        this.c1 = c1;
        this.d1 = d1;
    }
    //Sets the 2nd Linear equation.
    public void setLinearEquation2 (double a2, double b2, double c2, double d2){
        this.a2 = a2;
        this.b2 = b2;
        this.c2 = c2;
        this.d2 = d2;
    }
    //Sets the 3rd Linear Equation.
    public void setLinearEquation3 (double a3, double b3, double c3, double d3){
        this.a3 = a3;
        this.b3 = b3;
        this.c3 = c3;
        this.d3 = d3;
    }
    /*Returns the 3x3 Determinant */
    public double getDeterminant (){
        double d = a1 * ((b2 * c3) - (b3 * c2)) - a2 * ((b1 * c3) - (b3 * c1)) + a3 * ((b1 * c2) - (b2 * c1));
        return d;
    }
    /*Returns the 3x3 Determinant for x */
    public double getDx(){
        double x = d1 * ((b2 * c3) - (b3 * c2)) - b1 * ((d2 * c3) - (d3 * c2)) + c1 * ((d2 * b3) - (d3 * b2));
        return x;
    }
    /*Returns the 3x3 Determinant for y */
    public double getDy(){
        double y = a1 * ((d2 * c3) - (d3 * c2)) - d1 * ((a2 * c3) - (a3 * c2)) + c1 * ((a2 * d3) - (a3 * d2));
        return y;
    }
    /*Returns the 3x3 Determinant for z */
    public double getDz(){
        double z = a1 * ((b2 * d3) - (b3 * d2)) - b1 * ((a2 * d3) - (a3 * d2)) + d1 * ((a2 * b3) - (a3 * b2));
        return z;
    }
}

public class MyProgram{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        
        CramersRule CR1 = new CramersRule();
        System.out.print("Enter 4 numbers for the first equation (ie. 1 2 3 4): ");
        CR1.setLinearEquation1(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        
        CramersRule CR2 = new CramersRule();
        System.out.print("Enter 4 numbers for the second equation (ie. 1 2 3 4): ");
        CR2.setLinearEquation2(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        
        CramersRule CR3 = new CramersRule();
        System.out.print("Enter 4 numbers for the third equation (ie. 1 2 3 4): ");
        CR3.setLinearEquation3(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        
        
        System.out.println("The answer of the 3x3 Determinant is " + CR1.getDeterminant());
        if(CR1.getDeterminant() == 0){
            System.out.println("Cramers Rule does not apply.");
        }else{
            double x = CR1.getDeterminant() / CR1.getDx();
            double y = CR1.getDeterminant() / CR1.getDx();
            double z = CR1.getDeterminant() / CR1.getDx();
            
            System.out.println("The solution set is (" + x + ", " + y + ", " + z + ")");
        }
    }
}

问题根源

  1. 实例滥用导致参数缺失:创建了3个CramersRule实例,每个实例仅存储一个方程的参数(比如CR1只有a1、b1、c1、d1,a2、a3、b2等变量都是默认的0值)。调用CR1.getDeterminant()时,大量参与计算的变量为0,自然会得出错误的0结果。
  2. 解的计算公式错误:克莱姆法则的正确公式是x = Dx/D、y = Dy/D、z = Dz/D,但代码中写成了D/Dx,且三个解都误用了Dx。
  3. 浮点数精度判断问题:直接用==0判断行列式是否为0,会因浮点数精度误差导致误判,应该用绝对值小于极小值的方式判断。

修正后的代码

import java.util.Scanner;
class CramersRule{
    private double a1, a2, a3;
    private double b1, b2, b3;
    private double c1, c2, c3;
    private double d1, d2, d3;
    
    public void setLinearEquation1 (double a1, double b1, double c1, double d1){
        this.a1 = a1;
        this.b1 = b1;
        this.c1 = c1;
        this.d1 = d1;
    }
    public void setLinearEquation2 (double a2, double b2, double c2, double d2){
        this.a2 = a2;
        this.b2 = b2;
        this.c2 = c2;
        this.d2 = d2;
    }
    public void setLinearEquation3 (double a3, double b3, double c3, double d3){
        this.a3 = a3;
        this.b3 = b3;
        this.c3 = c3;
        this.d3 = d3;
    }
    public double getDeterminant (){
        return a1 * (b2 * c3 - b3 * c2) - a2 * (b1 * c3 - b3 * c1) + a3 * (b1 * c2 - b2 * c1);
    }
    public double getDx(){
        return d1 * (b2 * c3 - b3 * c2) - b1 * (d2 * c3 - d3 * c2) + c1 * (d2 * b3 - d3 * b2);
    }
    public double getDy(){
        return a1 * (d2 * c3 - d3 * c2) - d1 * (a2 * c3 - a3 * c2) + c1 * (a2 * d3 - a3 * d2);
    }
    public double getDz(){
        return a1 * (b2 * d3 - b3 * d2) - b1 * (a2 * d3 - a3 * d2) + d1 * (a2 * b3 - a3 * b2);
    }
}

public class MyProgram{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        
        // 仅用一个实例存储所有方程参数
        CramersRule cr = new CramersRule();
        
        System.out.print("Enter 4 numbers for the first equation (ie. 1 2 3 4): ");
        cr.setLinearEquation1(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        
        System.out.print("Enter 4 numbers for the second equation (ie. 1 2 3 4): ");
        cr.setLinearEquation2(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        
        System.out.print("Enter 4 numbers for the third equation (ie. 1 2 3 4): ");
        cr.setLinearEquation3(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        
        double determinant = cr.getDeterminant();
        System.out.println("The answer of the 3x3 Determinant is " + determinant);
        
        // 用绝对值判断是否接近0,避免精度问题
        if(Math.abs(determinant) < 1e-9){
            System.out.println("Cramers Rule does not apply.");
        }else{
            double x = cr.getDx() / determinant;
            double y = cr.getDy() / determinant;
            double z = cr.getDz() / determinant;
            
            System.out.printf("The solution set is (%.4f, %.4f, %.4f)%n", x, y, z);
        }
        
        input.close();
    }
}

修正说明

  • 仅使用一个CramersRule实例,确保所有方程参数都被正确存储到同一个实例中,保证行列式计算时能获取到完整的矩阵数据。
  • 修正了解的计算公式,符合克莱姆法则的定义。
  • 替换浮点数等于0的判断为Math.abs(determinant) < 1e-9,避免因精度误差导致的误判。
  • 增加input.close()关闭输入流,优化代码规范。

内容的提问来源于stack exchange,提问作者Caleb Murnan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 01:20:35