How to Solve Equations with java?

前端 未结 8 713
花落未央
花落未央 2020-12-03 05:45

I have three equations like the following ones:

  • x + y + z = 100;
  • x + y - z = 50;
  • x - y - z = 10;

How can I find the values of

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 06:12

    There are many ways to solve linear system equations. There is a simplest way to perform this. In the example the java code solve for two variables USING Matrix method but you can modify to perform 3 variables calculations.

    import java.util.Scanner; //OBJETO SCANNER
    
    public class SYS2 {
    
        public static void main (String args[]) {
    
            //VARIABLE DECLARATION SPACE
            int i=0,j = 0;
            float x,y;
             Scanner S = new Scanner (System.in);
    
             int EC3[][]= new int [2][3]; //ARRAY TO STORE EQUATION 2X3
    
             float DET1=0;
            float DET2 =0;
    
    
            float DETA=0;
             float DETB=0;
    
            //END VARIABLE DECLARATIONS
           System.out.println("Enter Equation System : ");
    
    for (i=0; i< 2; i++) {
                    for (j=0; j< 3; j++) 
                            EC3[i][j] = S.nextInt();
                    }    
    
                    System.out.println("SISTEMA DE ECUACION LINEAL: "); //THIS SENTENCE ONLY PRINT THE CATCHED VALUES OF EQUATION
    
                    for (i=0; i< 2; i++) {
                            for (j=0; j< 3; j++) 
    
                                  System.out.print(EC3[i][j] + "  ");
    
                                System.out.println();
    
    
                    }    
    
    
    
    
               //    System.out.print("Determinante A de la Matriz: ");
    
                 //    System.out.print((EC3[0][2] * EC3[1][1]) - (EC3[0][1]*EC3[1][2]) );
    
                    for (i=0;i<2;i++) {
                            for (j=0; j<2;j++)
                                    DET1=  ((EC3[0][2] * EC3[1][1]) -( EC3[0][1]*EC3[1][2]));
                    }
    
                               //    System.out.print(DET1 );
                                //       System.out.println();
    
                                     for (i=0;i<2;i++) {
                                            for (j=0; j<2;j++)
                                             DET2=  ((EC3[0][0] * EC3[1][1]) - (EC3[0][1]*EC3[1][0]));
                    }
    
                                   // System.out.print("Determinante B de la Matriz: ");
                                   //  System.out.println(DET2 ); 
    
        x = (DET1 / DET2);
    
        System.out.println();
        System.out.println("X = " + x);
        System.out.print("=======================");
        //FIN PARA VALOR DE X
    
    
    
        //COMIENZO DE VALOR DE Y
    
      //  System.out.print("Determinante A de la Matriz Y: ");
    
                                        for (i=0;i<2;i++) {
                                                for (j=0; j<2;j++)
                                             DETA=  EC3[0][0] * EC3[1][2] - EC3[0][2]*EC3[1][0];
    
    
                                            //    System.out.print(DETA );
                                              //  System.out.println();
                    }
    
    
                                         for (i=0;i<2;i++) {
                                                for (j=0; j<2;j++)
                                             DETB=  EC3[0][0] * EC3[1][1] - EC3[0][1]*EC3[1][0];
    
                    }
    
                                       // System.out.print("Determinante B de la Matriz Y: ");
                                       //  System.out.println(DETB );                  
                                        y = DETA / DETB;
    
                                        System.out.print("=======================");
                                        System.out.println();
    
                                        System.out.println("Y = " + y);
                                        System.out.print("=======================");
        }
    }
    
    
    
    
    

提交回复
热议问题