格式化以显示java中数字的正号或负号

时间:2015-10-08 14:11:15

标签: java netbeans format

美好的一天!我制作了一个程序,可以找到一个点的轨迹方程式,如果它是一个正数,我想让我的输出显示一个“+”符号。然而,它显示了一个“ - ”符号,因为我回报了他们的价值观。这是我的代码:

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

        int x1 , y1, x2, y2,
            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" + Integer.toString(inverted_x1) + ")² + (y" + Integer.toString(inverted_y1) + ")²" 
                    + " = (x" + Integer.toString(inverted_x2) + ")² + (y" + Integer.toString(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" + Integer.toString(x1Times2) + "x" + Integer.toString(x1raised) + "+y²" + Integer.toString(y1Times2)
                    + "y" + Integer.toString(y1raised) + "= x²" + Integer.toString(x2Times2) + "x" + Integer.toString(x2raised) + "+y²"
                    + Integer.toString(y2Times2) + "y" + Integer.toString(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(Integer.toString(computeX) + "x" + Integer.toString(computeY) + "y" + Integer.toString(computeS) + "=0");                                                                                 

            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");
        }
    }      

以下是示例输出: Click Me。如果它的正面,我可以让我的输出显示“+”符号吗?

提前谢谢!

0 个答案:

没有答案