2
我必須作出一個程序,它從具有n列的二維陣列和2行的前輸入的:以餾分=(1 2 3 4) (5 6 7 8)數組運算符
它也有(例如:Operators = [+ * - ])
代碼必須根據1d數組中的運算符對數組中的分數進行加,減,乘,除 - 例如: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;
}
}
它總是2行嗎? – JordanGS
我正在研究它,但這對於atm來說工作太多了,如果您創建一個名爲'Fraction'的新類,那麼最簡單。看到這裏[信息](http://stackoverflow.com/questions/8453485/addition-subtraction-multiplication-division-with-fractions-in-java-homewo) – JordanGS