Java 数学完成平方计算器

Java Math Completing the square calculator

嘿,我是整个编程领域的新手,我只是一名正在上 java 计算机科学课程的高中生。我试图用我目前所知道的来测试我的能力,并尝试理解事物和实践。所以我正在尝试为某些事情制作几个数学计算器。我正在研究一个以完成广场。所以像 (2 + 4x^2)^2 会变成 (4 + 16x + 4x^2)。我的问题是我无法让它完全工作,而且代码有点笨拙。

import java.util.Scanner;

public class SquaringDoubles {

    public static void main(String[] args) {

        //declaring
        Scanner input = new Scanner(System.in);

        //inputs
        System.out.println("Enter in the double with this format ( A + B )^2");

        System.out.print("A --> ");
        double A = input.nextInt();

        System.out.print("B --> ");
        double B = input.nextInt();
        input.close();

        System.out.println("You're Equation: " + A + " + " + B + "x");

        //Math
        //A + C + B
        double A2 = Math.pow(A, 2);
        double B2 = Math.pow(B, 2);
        double C = 2 * (A * B);

        //final
        System.out.print("You answer: ");
        System.out.println(A2 + " + "+ C + "x" + " + "+ B2+ "x^2");
    }
}

插入是从一个公式到等价形式方程的最简单的通用方法

Y(x) = A2 + B2x + C2(x)^2

由于有三个变量(A2、B2、C2),我们需要三个方程来求解系统。 为了得到这些方程,我们可以简单地将我们选择的三个 x 和它们计算出的 Y(x) 放入我们想要的形式。并求解方程组。

所以本质上,我们为我们选择的三个 x 计算 Y(x),然后将它们粘贴到公式中。可以采用任何(定义的)值,但有些值会使生活更轻松。

所以

X=0 是第一个候选,因为它用 x 消除了所有内容并直接给你 A2。

Y(0)= C2*(0)^2+B2*(0)+A2 = A2
A2 = Y(0)

x=1: 你得到

 Y(1)= C2*(1)^2+B2*(1)+A2 = C2+B2+A2

x=-1: 你得到

Y(-1)= C2*(-1)^2+B2*(-1)+A2= C2-B2+A2

消除 C2:

Y(-1)+B2-A2 = Y(1) -B2 -A2
-> 2*B2=Y(1)-Y(-1)
B2=(Y(1)-Y(-1))/2

最后通过插入C2+B2+A2=Y(1)来计算C2:

C2=Y(1) -B2 -A2

So in General - for any given(valid) equation to get to the form A2+B2·x+C2·x²:

  • A2 = Y(0)
  • B2=(Y(1)-Y(-1))/2
  • C2=Y(1) -B2 -A2 = (Y(1)+Y(-1))/2 - Y(0)

在你的例子中 Y(1) = Y(-1) 由于平方,所以

Y(x) = (A + B * (x)^2)^2

Y(1) = (A + B * (1)^2)^2   =(A+B)^2  // x=1
Y(-1) = (A + B * (-1)^2)^2 =(A+B)^2  // x=-1
B2 =(Y(1)-Y(-1))/2 =0

等等

C2= Y(1) -A2 = (A+B)^2 - A2

So for (A + B*(x)^2)^2:

  • A2 = Y(0) = A^2
  • B2=0
  • C2=Y(1) - Y(0) = (A+B)^2 - A2

代码:

public static void main(String[] args) {

        //declaring
        Scanner input = new Scanner(System.in);

        //inputs
        System.out.println("Enter in the double with this format (A + B(x)^2)^2");

        System.out.print("A --> ");
        double A = input.nextInt();

        System.out.print("B --> ");
        double B = input.nextInt();
        input.close();

        System.out.println("You're Equation: (" + A + " + " + B + "x^2)^2");

        //Math
        //A + C + B
        double A2 = Math.pow(A, 2);
        /**  old code:
         * 
        double B2 = Math.pow(B, 2);

        double C = 2 * (A * B);
        */     
        /** replacement : */
        //Y(1)=(A + B*(1)^2)^2 = (A+B)^2
        //Y(-1)=(A + B*(-1)^2)^2 = (A+B)^2
        //B2 = (Y(1)-Y(-1))/2 = ((A+B)^2 -(A+B)^2)/2 = 0
        double B2=0; // it is always 0 in this case
        //Y(1)=(A + B*(1)^2)^2 = (A+B)^2
        double C2=(A+B)*(A+B) - A2; //Y(1) -A2 
        //final
        System.out.print("You answer: ");
        System.out.println(A2 + " + " + B2 + "x" + " + " + C2 + "x^2");
    }

我添加了一些评论,以展示正在发生的事情以及如何对其他方程进行类似处理。