寻找GCD java

时间:2015-10-08 18:04:05

标签: java

美好的一天!我的代码有问题。它有一个错误,这里是图片:enter image description here

这是我的代码:

private static int gcd(int a, int b, int c) {
        return gcd(gcd(a,b),c);
    }


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        //DecimalFormat fmt = new DecimalFormat("+##");
        int x1 , y1, x2, y2, gcd,
            inverted_x1, inverted_y1, inverted_x2, inverted_y2,
            x1Times2, x2Times2, y1Times2, y2Times2,
            x1raised, y1raised, x2raised, y2raised,
            computeX, computeY, computeS,
            x2inverted, y2inverted, invertedx2times2, invertedy2times2;

        try{
            x1 = Integer.parseInt(jTextField1.getText());
            y1 = Integer.parseInt(jTextField2.getText());
            x2 = Integer.parseInt(jTextField3.getText());
            y2 = Integer.parseInt(jTextField4.getText());           

            int[] array = new int[4];
                  array[0] = x1;
                  array[1] = x2;
                  array[2] = y1;
                  array[3] = y2;

            inverted_x1 = x1 *= -1;
            inverted_y1 = y1 *= -1;
            inverted_x2 = x2 *= -1;      
            inverted_y2 = y2 *= -1;                                 

            jTextField5.setText("= (x" + formatSign(inverted_x1)  + ")² + (y" + formatSign(inverted_y1) + ")²" 
                    + " = (x" + formatSign(inverted_x2) + ")² + (y" + formatSign(inverted_y2) + ")²" );

            x1Times2 = inverted_x1*2;
            y1Times2 = inverted_y1*2;           
            x2Times2 = inverted_x2*2;
            y2Times2 = inverted_y2*2;                                   
            x1raised = inverted_x1*inverted_x1;
            y1raised = inverted_y1*inverted_y1;         
            x2raised = inverted_x2*inverted_x2;
            y2raised = inverted_y2*inverted_y2;

            jTextField9.setText("= x²" + formatSign(x1Times2) + "x" + formatSign(x1raised) + "+y²" + formatSign(y1Times2)
                    + "y" + formatSign(y1raised) + "= x²" + formatSign(x2Times2) + "x" + formatSign(x2raised) + "+y²"
                    + formatSign(y2Times2) + "y" + formatSign(y2raised));            

            x2inverted = x2raised *= -1;
            y2inverted = y2raised *= -1;
            invertedx2times2 = x2Times2 *= -1;
            invertedy2times2 = y2Times2 *= -1;

            computeX = x1Times2 + invertedx2times2;
            computeY = y1Times2 + invertedy2times2;
            computeS = x2inverted + y2inverted + x1raised + y1raised;           

            jTextField17.setText("= " + formatSign(computeX) + "x" + formatSign(computeY) + "y" + formatSign(computeS) + "= 0");      

            gcd = gcd(computeX, computeY, computeS);
            jTextField18.setText("= " + formatSign(computeX/gcd) + "x" + formatSign(computeY/gcd) + "y" + formatSign(computeS/gcd) + "= 0");   

            jTextField6.setText("= " + formatSign(10/gcd));

            XYLineChart_AWT chart = new XYLineChart_AWT("Locus of Point Graph", "", array);
            chart.pack();          
            RefineryUtilities.centerFrameOnScreen( chart );          
            chart.setVisible( true );             

        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Please fill necessary inputs");
        }
    }

错误显示在我的返回gcd(gcd(a,b),c);

我希望获得3个数字的GCD,然后将其除以我的变量。还有其他方法吗?或者有办法解决错误吗?

提前致谢!

1 个答案:

答案 0 :(得分:2)

因为gcd(...)需要3个参数

 private static int gcd(int a, int b, int c) {
            return gcd(gcd(a,b),c);
        }

return中,您只传递了2.此外,根据您显示的代码,如果您传递3参数,则它将是infinite recursion。感谢@eis指出这一点。我忘了提它:)