简体   繁体   English

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

[英]Print out the quadratic equation given its coefficients.

I have this program that solves quadratic equations, but whenever I try to output the equation after given the coefficients, I need to to print in the form ax^2 +/- bx +/c. 我有这个解决二次方程的程序,但每当我在给定系数后尝试输出方程时,我需要以ax ^ 2 +/- bx + / c的形式打印。 How can I get my equation to print with the sign of the number as the "+/-"? 如何使用数字符号作为“+/-”来打印我的等式?

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



        }

    }

If you want to print out a special character like ± . 如果你想打印出像±这样的特殊字符。

You can iterate through the list of characters like: 您可以遍历以下字符列表:

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

At my side when I print: 我打印时在我身边:

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

It gives me ± . 它给了我±

However , I think this may be quite unsafe, as the symbol showing on your screen may not be the same when run on another work station. 但是 ,我认为这可能非常不安全,因为在另一个工作站上运行时,屏幕上显示的符号可能不一样。

Use this %+.5f in System.out.printf to print the sign of number System.out.printf使用此%+.5f打印数字符号

'+' flag is used for Numeric values to display its sign '+'标志用于数值以显示其符号

Somewhat like 有点像

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

You can include the sign of the number in the format string by including a + 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 打印Equation: 1.00000x^2 -2.00000x +3.00000 = 0.00000

If you want proper spacing (eg 1.0 - 2.0 + 3.0 ), you can instead have the sign as a separate character that you set based on the sign of the number. 如果你想要适当的间距(例如1.0 - 2.0 + 3.0 ),你可以将标志作为你根据数字符号设置的单独字符。

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 打印Equation: 1.00000x^2 - 2.00000x + 3.00000 = 0.00000

( Math.abs() calls are there so it doesn't print eg 1.0 - -2.0 ) Math.abs()调用在那里,因此它不打印,例如1.0 - -2.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM