给出其系数,打印出二次方程。

时间:2015-09-25 13:36:11

标签: java

我有这个解决二次方程的程序,但每当我在给定系数后尝试输出方程时,我需要以ax ^ 2 +/- bx + / c的形式打印。如何使用数字符号作为“+/-”来打印我的等式?

 package quadraticsolver;
    import java.util.Scanner;
    /**
     *
     * @author Trevor
     */
    public class QuadraticSolver
    {
        public static void main(String[] args)
        {
           Scanner a = new Scanner(System.in);
           Scanner b = new Scanner(System.in);
           Scanner c = new Scanner(System.in);
           float qCoeff, lCoeff, constant, discriminant, x1, x2, x3, x4;

           System.out.print("Enter the coefficient of the quadratic term -> ");
           qCoeff = a.nextFloat();
           System.out.print("Enter the coefficient of the linear term -> ");
           lCoeff = b.nextFloat();
           System.out.print("Enter the constant term -> ");
           constant = c.nextFloat();

           discriminant = (float) (Math.pow(lCoeff,2) - (4*qCoeff*constant));

           if (qCoeff == 0)
           {
               System.out.println("The equation must have a non-zero quadratic term.");
           }               
           else if (discriminant == 0)
           {
               x1 = (float) (-1*lCoeff)/(2*qCoeff);
               System.out.printf("Equation: %.5fx^2+%.5fx+ %.5f = 0.00000%n", qCoeff, lCoeff, constant);
               System.out.printf("Solution: x={%.5f}", x1);
           }
           else if (discriminant > 0)
           {
               x1 = (float) ((-1*lCoeff+Math.sqrt(discriminant))/(2*qCoeff));
               x2 = (float) ((-1*lCoeff-Math.sqrt(discriminant))/(2*qCoeff));
               System.out.printf("Equation: %.5fx^2 + %.5fx + %.5f = 0.00000%n", qCoeff, lCoeff, constant);
               System.out.printf("Solution: x={%.5f, %.5f%n}", x1, x2);


           }
           else if (discriminant <0)
           {
               x1 = (float) (-1*lCoeff)/(2*qCoeff);
               x2 = (float) Math.abs(Math.sqrt((Math.abs(discriminant)))/(2*qCoeff));
               x3 = (float) (-1*lCoeff)/(2*qCoeff);
               x4 = (float) Math.abs(Math.sqrt((Math.abs(discriminant))/(2*qCoeff)));
               System.out.printf("Equation: %.5fx^2+%.5fx+%.5f = 0.00000%n", qCoeff, lCoeff, constant);
               System.out.printf("Solution: x={%.5f+%.5fi, %.5f-%.5fi}", x1, x2, x3, x4);

           }
           else
               System.out.println("Invalid type. Please input a number");



        }

    }

3 个答案:

答案 0 :(得分:2)

如果您想打印出±等特殊字符。

您可以遍历以下字符列表:

for(int x=0; x<256; x++)
    System.out.println("Symbol of " + x + ": " + ((char)x)); 

我打印时在我身边:

System.out.println(((char)177));

它给了我±

然而,我认为这可能非常不安全,因为在另一个工作站上运行时,屏幕上显示的符号可能不一样。

答案 1 :(得分:1)

%+.5f中使用此System.out.printf打印数字符号

'+'标志用于数字值以显示其符号

有点像

System.out.printf("Equation: %+.5fx^2 %+.5fx %+.5f = 0.00000%n", qCoeff, lCoeff, constant);

答案 2 :(得分:1)

您可以在格式字符串中包含数字的符号,方法是在格式说明符中包含+。例如,

System.out.printf("Equation: %.5fx^2 %+.5fx %+.5f = 0.00000%n", 1.0, -2.0, 3.0);

打印Equation: 1.00000x^2 -2.00000x +3.00000 = 0.00000

如果您想要适当的间距(例如1.0 - 2.0 + 3.0),则可以将该符号设置为您根据数字符号设置的单独字符。

示例:

float qCoeff = 1.0f, lCoeff = -2.0f, constant = 3.0f;
char lSign = lCoeff < 0 ? '-' : '+';
char constSign = constant < 0 ? '-' : '+';

System.out.printf("Equation: %.5fx^2 %c %.5fx %c %.5f = 0.00000%n", 
                   qCoeff, lSign, Math.abs(lCoeff), constSign, Math.abs(constant));

打印Equation: 1.00000x^2 - 2.00000x + 3.00000 = 0.00000

Math.abs()有来电,所以它不打印,例如1.0 - -2.0