Print the quadratic equation with its coefficients.

I have this program that solves quadratic equations, but whenever I try to derive an equation after given coefficients, I need to print in the form ax ^ 2 +/- bx + / c. How can I get my equation to print with the number sign as "+/-"?

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



        }

    }
+4
source share
3 answers

You can include the number sign in the format string by specifying +in the format specifier. For example,

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

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

If you need the right spacing (for example 1.0 - 2.0 + 3.0), you can instead have the character as a separate character, which you set based on the number sign.

Example:

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

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

(Math.abs() , , , 1.0 - -2.0)

+1

, ±.

, :

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

, :

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

±.

, , , , , .

+2

%+.5f System.out.printf

'+'

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

Source: https://habr.com/ru/post/1608996/


All Articles