Java 二次方程系数计算

Java 二次方程系数计算,java,quadratic,Java,Quadratic,关于二次方程(了解更多),我将方程的a、b和c作为输入 示例公式如下:21x^2-8x-4 这里,a=21,b=-8,c=-4。因此,在求解(无公式)时, =>21x^2-14x+6x-4=0 我需要两个中间数字,也就是说,在这个例子中是14和6(读取因子)。我认为我所做的一切都是正确的,但输入似乎是无限的,而且一点也没有停止。你能纠正这个错误吗?我也很想知道为什么会这样 import java.util.Scanner; public class QuadFact { static S

关于二次方程(了解更多),我将方程的
a
b
c
作为输入

示例公式如下:21x^2-8x-4 这里,a=21,b=-8,c=-4。因此,在求解(无公式)时, =>21x^2-14x+6x-4=0

我需要两个中间数字,也就是说,在这个例子中是14和6(读取因子)。我认为我所做的一切都是正确的,但输入似乎是无限的,而且一点也没有停止。你能纠正这个错误吗?我也很想知道为什么会这样

import java.util.Scanner;
public class QuadFact {
    static Scanner sc = new Scanner(System.in); 
    static int a,b,c; 
    static int P, diff, p; 
    static int i;
    static boolean found = false;

    void accept(){
        System.out.println("Enter the a, b, c");
        a = sc.nextInt(); b = sc.nextInt(); c = sc.nextInt();
    }

    void compute(){
        P = a * c;
        diff = 0;
        while(!found){
           for (i = b + 1;;i++){
                diff = i - b;
                p = i * diff;
                if (p==P) {
                    found = true;
                    break; 
                }
            }
        }
    }

    void display(){
        System.out.print("These are the raw numbers, should be   correct.  
        Still,\n it is advisable you verify it.");
        System.out.println("One factor: " + i);
        System.out.println("Other factor: " + diff);
    }

    public static void main(String[] args){
        QuadFact a = new QuadFact();
        a.accept();
        a.compute();
        a.display();
    }
}
我认为你必须在b的“两面”寻找一个因子对,它加起来等于b,并产生乘积a*c

void compute(){
    P = a * c;
    while(!found){
    for( i = 1; ; i++ ){
            diff = b - i;
            if (i * diff == P) {
                found = true;
                break; 
            }
            diff = b + i;
            if (-i * diff == P) {
                found = true;
                break; 
            }
        }
    }
}
我认为你必须在b的“两面”寻找一个因子对,它加起来等于b,并产生乘积a*c

void compute(){
    P = a * c;
    while(!found){
    for( i = 1; ; i++ ){
            diff = b - i;
            if (i * diff == P) {
                found = true;
                break; 
            }
            diff = b + i;
            if (-i * diff == P) {
                found = true;
                break; 
            }
        }
    }
}
我认为你必须在b的“两面”寻找一个因子对,它加起来等于b,并产生乘积a*c

void compute(){
    P = a * c;
    while(!found){
    for( i = 1; ; i++ ){
            diff = b - i;
            if (i * diff == P) {
                found = true;
                break; 
            }
            diff = b + i;
            if (-i * diff == P) {
                found = true;
                break; 
            }
        }
    }
}
我认为你必须在b的“两面”寻找一个因子对,它加起来等于b,并产生乘积a*c

void compute(){
    P = a * c;
    while(!found){
    for( i = 1; ; i++ ){
            diff = b - i;
            if (i * diff == P) {
                found = true;
                break; 
            }
            diff = b + i;
            if (-i * diff == P) {
                found = true;
                break; 
            }
        }
    }
}

好的,我为此写了一个代码

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Declare and get the variables 
        int a, b,c;

        Scanner s = new Scanner(System.in);

        System.out.println("Enter A");

        a = s.nextInt();

        System.out.println("Enter B");

        b = s.nextInt(); 

        System.out.println("Enter c");

        c = s.nextInt(); 

        //A should be 1 if not divide a, b and c by a
        if (a>1) {

            b= b/a;
            c=c/a;
            a= a/a;
        }
        //Just printing what the values of ABC IS AGAIN
        System.out.println("A = "+a+" B = "+b+" C = "+c);
        //Just printing what the values of ABC IS AGAIN but in reverse, that is if c was 5 it becomes -5 
        //another way to reverse positive to negative and vice versa
        System.out.println("A = "+(0-a)+" B = "+(0-b)+" C = "+(0-c));

        //Set i as c and start the loop from highest to lowest. 
        for (int i = Math.abs(c); i>0 ;i-- ) {

            //if a multiple is found it proceeds and checks for the 
            //multiple combination that when multiplied you get C and the addition or subtraction gives you B
            if (c%i==0) { 
                int fac1 = c/i;
                //Displays the multiples found

                System.out.println(i+" x "+fac1+" = "+c);

                //There are 4 possible outcomes or cases
                //case 1 multiple 1 - multiple 2 = b
                //case 2 multiple 2 - multiple 1 = b
                //case 3 multiple 1 + multiple 2 = b
                //case 4 -multiple 1 + -multiple 2 = b

                if (i-fac1 == b) {
                    //System.out.println("case 1: " + i+"-"+fac1+"="+b);
                    answer(i,fac1);
                    break;
                }
                else if(fac1 - i == b){

                    //System.out.println("case2: "  + fac1+"-"+i+"="+b);
                    answer(fac1,i);
                    break;
                }

                else if (i +fac1 == b ) {

                    //System.out.println("case3: "  + i+"+"+fac1+"="+b);
                    answer(i,fac1);
                    break;
                }
                else if((0-Math.abs(i)) + (0- Math.abs(fac1))==b) {

                    //System.out.println("case4: "+"-"+i+ " + "+ "-"+ fac1 +"="+b);

                    answer((0-Math.abs(i)),(0- Math.abs(fac1)));
                    break;
                }


                else {
                    System.out.println("Probably not a factorizable Equation");
                }

            }

        }

    }
    //Use this method to show the final answer
    private static void answer(int f1, int f2) {

        System.out.println("x = "+(0-f1) +" or x = "+(0-f2));

    }

}

好的,我为此写了一个代码

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Declare and get the variables 
        int a, b,c;

        Scanner s = new Scanner(System.in);

        System.out.println("Enter A");

        a = s.nextInt();

        System.out.println("Enter B");

        b = s.nextInt(); 

        System.out.println("Enter c");

        c = s.nextInt(); 

        //A should be 1 if not divide a, b and c by a
        if (a>1) {

            b= b/a;
            c=c/a;
            a= a/a;
        }
        //Just printing what the values of ABC IS AGAIN
        System.out.println("A = "+a+" B = "+b+" C = "+c);
        //Just printing what the values of ABC IS AGAIN but in reverse, that is if c was 5 it becomes -5 
        //another way to reverse positive to negative and vice versa
        System.out.println("A = "+(0-a)+" B = "+(0-b)+" C = "+(0-c));

        //Set i as c and start the loop from highest to lowest. 
        for (int i = Math.abs(c); i>0 ;i-- ) {

            //if a multiple is found it proceeds and checks for the 
            //multiple combination that when multiplied you get C and the addition or subtraction gives you B
            if (c%i==0) { 
                int fac1 = c/i;
                //Displays the multiples found

                System.out.println(i+" x "+fac1+" = "+c);

                //There are 4 possible outcomes or cases
                //case 1 multiple 1 - multiple 2 = b
                //case 2 multiple 2 - multiple 1 = b
                //case 3 multiple 1 + multiple 2 = b
                //case 4 -multiple 1 + -multiple 2 = b

                if (i-fac1 == b) {
                    //System.out.println("case 1: " + i+"-"+fac1+"="+b);
                    answer(i,fac1);
                    break;
                }
                else if(fac1 - i == b){

                    //System.out.println("case2: "  + fac1+"-"+i+"="+b);
                    answer(fac1,i);
                    break;
                }

                else if (i +fac1 == b ) {

                    //System.out.println("case3: "  + i+"+"+fac1+"="+b);
                    answer(i,fac1);
                    break;
                }
                else if((0-Math.abs(i)) + (0- Math.abs(fac1))==b) {

                    //System.out.println("case4: "+"-"+i+ " + "+ "-"+ fac1 +"="+b);

                    answer((0-Math.abs(i)),(0- Math.abs(fac1)));
                    break;
                }


                else {
                    System.out.println("Probably not a factorizable Equation");
                }

            }

        }

    }
    //Use this method to show the final answer
    private static void answer(int f1, int f2) {

        System.out.println("x = "+(0-f1) +" or x = "+(0-f2));

    }

}

好的,我为此写了一个代码

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Declare and get the variables 
        int a, b,c;

        Scanner s = new Scanner(System.in);

        System.out.println("Enter A");

        a = s.nextInt();

        System.out.println("Enter B");

        b = s.nextInt(); 

        System.out.println("Enter c");

        c = s.nextInt(); 

        //A should be 1 if not divide a, b and c by a
        if (a>1) {

            b= b/a;
            c=c/a;
            a= a/a;
        }
        //Just printing what the values of ABC IS AGAIN
        System.out.println("A = "+a+" B = "+b+" C = "+c);
        //Just printing what the values of ABC IS AGAIN but in reverse, that is if c was 5 it becomes -5 
        //another way to reverse positive to negative and vice versa
        System.out.println("A = "+(0-a)+" B = "+(0-b)+" C = "+(0-c));

        //Set i as c and start the loop from highest to lowest. 
        for (int i = Math.abs(c); i>0 ;i-- ) {

            //if a multiple is found it proceeds and checks for the 
            //multiple combination that when multiplied you get C and the addition or subtraction gives you B
            if (c%i==0) { 
                int fac1 = c/i;
                //Displays the multiples found

                System.out.println(i+" x "+fac1+" = "+c);

                //There are 4 possible outcomes or cases
                //case 1 multiple 1 - multiple 2 = b
                //case 2 multiple 2 - multiple 1 = b
                //case 3 multiple 1 + multiple 2 = b
                //case 4 -multiple 1 + -multiple 2 = b

                if (i-fac1 == b) {
                    //System.out.println("case 1: " + i+"-"+fac1+"="+b);
                    answer(i,fac1);
                    break;
                }
                else if(fac1 - i == b){

                    //System.out.println("case2: "  + fac1+"-"+i+"="+b);
                    answer(fac1,i);
                    break;
                }

                else if (i +fac1 == b ) {

                    //System.out.println("case3: "  + i+"+"+fac1+"="+b);
                    answer(i,fac1);
                    break;
                }
                else if((0-Math.abs(i)) + (0- Math.abs(fac1))==b) {

                    //System.out.println("case4: "+"-"+i+ " + "+ "-"+ fac1 +"="+b);

                    answer((0-Math.abs(i)),(0- Math.abs(fac1)));
                    break;
                }


                else {
                    System.out.println("Probably not a factorizable Equation");
                }

            }

        }

    }
    //Use this method to show the final answer
    private static void answer(int f1, int f2) {

        System.out.println("x = "+(0-f1) +" or x = "+(0-f2));

    }

}

好的,我为此写了一个代码

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Declare and get the variables 
        int a, b,c;

        Scanner s = new Scanner(System.in);

        System.out.println("Enter A");

        a = s.nextInt();

        System.out.println("Enter B");

        b = s.nextInt(); 

        System.out.println("Enter c");

        c = s.nextInt(); 

        //A should be 1 if not divide a, b and c by a
        if (a>1) {

            b= b/a;
            c=c/a;
            a= a/a;
        }
        //Just printing what the values of ABC IS AGAIN
        System.out.println("A = "+a+" B = "+b+" C = "+c);
        //Just printing what the values of ABC IS AGAIN but in reverse, that is if c was 5 it becomes -5 
        //another way to reverse positive to negative and vice versa
        System.out.println("A = "+(0-a)+" B = "+(0-b)+" C = "+(0-c));

        //Set i as c and start the loop from highest to lowest. 
        for (int i = Math.abs(c); i>0 ;i-- ) {

            //if a multiple is found it proceeds and checks for the 
            //multiple combination that when multiplied you get C and the addition or subtraction gives you B
            if (c%i==0) { 
                int fac1 = c/i;
                //Displays the multiples found

                System.out.println(i+" x "+fac1+" = "+c);

                //There are 4 possible outcomes or cases
                //case 1 multiple 1 - multiple 2 = b
                //case 2 multiple 2 - multiple 1 = b
                //case 3 multiple 1 + multiple 2 = b
                //case 4 -multiple 1 + -multiple 2 = b

                if (i-fac1 == b) {
                    //System.out.println("case 1: " + i+"-"+fac1+"="+b);
                    answer(i,fac1);
                    break;
                }
                else if(fac1 - i == b){

                    //System.out.println("case2: "  + fac1+"-"+i+"="+b);
                    answer(fac1,i);
                    break;
                }

                else if (i +fac1 == b ) {

                    //System.out.println("case3: "  + i+"+"+fac1+"="+b);
                    answer(i,fac1);
                    break;
                }
                else if((0-Math.abs(i)) + (0- Math.abs(fac1))==b) {

                    //System.out.println("case4: "+"-"+i+ " + "+ "-"+ fac1 +"="+b);

                    answer((0-Math.abs(i)),(0- Math.abs(fac1)));
                    break;
                }


                else {
                    System.out.println("Probably not a factorizable Equation");
                }

            }

        }

    }
    //Use this method to show the final answer
    private static void answer(int f1, int f2) {

        System.out.println("x = "+(0-f1) +" or x = "+(0-f2));

    }

}


再次尝试在此处发布您的代码。我们可以帮你格式化。链接可能会失效,我们中的一些人(比如我自己)甚至无法访问您的链接。好吧,当您尝试使用调试器调试此链接时会发生什么情况,或者当您使用println语句查看您的代码在关键点执行什么操作时会发生什么情况?您已经弄糊涂了。在你的例子中,你想要的数字是-14和6,而不是14和6。你应该寻找两个乘积为P和和为b的数字,而你似乎在寻找乘积为P和差为b的数字。输入是无限量,再次在这里发布你的代码。我们可以帮你格式化。链接可能会失效,我们中的一些人(比如我自己)甚至无法访问您的链接。好吧,当您尝试使用调试器调试此链接时会发生什么情况,或者当您使用println语句查看您的代码在关键点执行什么操作时会发生什么情况?您已经弄糊涂了。在你的例子中,你想要的数字是-14和6,而不是14和6。你应该寻找两个乘积为P和和为b的数字,而你似乎在寻找乘积为P和差为b的数字。输入是无限量,再次在这里发布你的代码。我们可以帮你格式化。链接可能会失效,我们中的一些人(比如我自己)甚至无法访问您的链接。好吧,当您尝试使用调试器调试此链接时会发生什么情况,或者当您使用println语句查看您的代码在关键点执行什么操作时会发生什么情况?您已经弄糊涂了。在你的例子中,你想要的数字是-14和6,而不是14和6。你应该寻找两个乘积为P和和为b的数字,而你似乎在寻找乘积为P和差为b的数字。输入是无限量,再次在这里发布你的代码。我们可以帮你格式化。链接可能会失效,我们中的一些人(比如我自己)甚至无法访问您的链接。好吧,当您尝试使用调试器调试此链接时会发生什么情况,或者当您使用println语句查看您的代码在关键点执行什么操作时会发生什么情况?您已经弄糊涂了。在你的例子中,你想要的数字是-14和6,而不是14和6。你应该寻找两个乘积为P和和为b的数字,而你似乎在寻找乘积为P和差为b的数字。输入是无穷大的。这通常在a=1时有效,并且可以被c或b平均除。在类似三项式的情况下,它可能不起作用。注意,这主要是在a=1时起作用,并且可以被c或b平均分割。在类似三项式的情况下,它可能不起作用。注意,这主要是在a=1时起作用,并且可以被c或b平均分割。在类似三项式的情况下,它可能不起作用。注意,这主要是在a=1时起作用,并且可以被c或b平均分割。在类似三项式的情况下,它可能不起作用