java - 运算符数组

标签 java arrays operators lcd greatest-common-divisor

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

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

代码必须根据一维数组中的运算符对数组中的分数进行加、减、乘、除 - 例如: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
}
}

关于java - 运算符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40478629/

相关文章:

c# - 将 float[] 作为 ref float 传递给非托管代码是个好主意吗?

c - 读取文本文件并对两个数组进行排序

c# - System.Nullable<T> null * int值是什么值?

c++ - "new"运算符和 "new"函数之间的区别

javascript - 根据匹配的阿拉伯字母显示产品列表?

java - 数据库信息 -> 对象 : how should it be done?

java - 使用java从CLOB OUT参数从oracle中的存储过程获取值

java - 安装juno eclipse时出错

c - "return x == y"是什么意思?

java - JMS - CorrelationID 与 ReplyTo