运算符数组

Array of operators

我必须编写一个程序,该程序从具有 n 列和 2 行的二维数组中获取输入,例如:fractions=(1 2 3 4) (5 6 7 8)

它还必须接受一维运算符数组(例如:运算符 = [ + * − ])

代码必须根据一维数组中的运算符对数组中的分数进行加、减、乘、除 - 例如:1/5 + 2/6 * 3/7 -4/8

我的代码可以正确输入两个数组,但我很难弄清楚如何让它进行数学运算。我读过答案包含最小公倍数和最大公除数,所以我也为此做了一个单独的程序,但不知道如何将这些程序组合在一起。有没有人以前见过这样的问题并可以提供一些建议? 先感谢您。

import java.util.Scanner;
public class ProjectCS {
    public static void main (String[]args){
        Scanner s = new Scanner(System.in);


        //1D ARRAY OF OPERATORS 
        System.out.println("How many operators are you going to enter?");
        int length = s.nextInt();
        String operators[]=new String[length];

        System.out.println("Enter operators(+, -, *)");

        for (int counter=0; counter<length; counter++){
            operators[counter]=s.next();

            System.out.print(operators[counter]); 
            //1D ARRAY OF OPERATORS END 
        }


        //INPUT OF ROWS AND COLUMNS

        System.out.print("Enter number of rows in matrix:");

        int rows = s.nextInt();

        System.out.print("Enter number of columns in matrix:");

        int n = s.nextInt();

        int fractions[][] = new int[rows][n];

        System.out.println("Enter all the elements of matrix:");

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < n; j++) {
                fractions[i][j] = s.nextInt();
            }
        }   
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < n;j++){



               System.out.print (fractions[i][j]);

            }
            System.out.println("");

            //END OF INPUT OF ROWS AND COLUMNS


        }
    }


}
//LCM code and GCM code 

import java.util.Scanner;
public class LCM_GCD_METHOD {

    public static void printFace() {
        int a;
        int b;
        int LCM;
        int temp;
        int GCD;


        Scanner sc= new Scanner(System.in);
        int x=sc.nextInt();
        int y=sc.nextInt();

        a=x;
        b=y;


        while(b!=0){
            temp = b;
            b = a % b;
            a = temp;
        }
        GCD = a;
        LCM = (x*y)/GCD;

        System.out.println("GCD = " + GCD);
        System.out.println("LCM = " + LCM);   
    }
    public static void main (String [] args) {
        printFace();
        return;
    }
}

您的数字数组是整数数组,但您的运算符数组是字符数组。我认为最直接的方法是

for(int i = 0 ; i < operators.length ; i++){
if(operators[i] == '+'){
//do addition
}else if(operators[i] == '-'){
//do subtraction
//and more conditions for the remaining operators
}
}