2016-11-08 3 views
2

Ich habe ein Programm zu machen, die mit n Spalten und 2 Zeilen ex Eingabe von einem 2D-Array nimmt: Fraktionen = (1 2 3 4) (5 6 7 8)Array von Operatoren

Es hat sich auch Nehmen Sie ein 1d-Array von Operatoren (zB: Operatoren = [+ * -])

Der Code muss Brüche im Array hinzufügen, subtrahieren, multiplizieren, teilen, abhängig von den Operatoren im 1d-Array - ex: 1/5 + 2/6 * 3/7 -4/8

Ich habe meinen Code bekommen, um beide Arrays korrekt einzugeben, aber es fällt mir schwer, herauszufinden, wie man es zur Berechnung bringt. Ich habe gelesen, dass die Antwort am wenigsten gemeinsame multiple und größte gemeinsame Teiler enthält, so dass ich auch ein separates Programm dafür gemacht habe, aber nicht weiß, wie man die Programme miteinander kombiniert. Hat jemand schon mal so ein Problem gesehen und kann einen Rat geben? Vielen Dank im Voraus.

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; 
    } 
} 
+0

ist es immer 2 Reihen? – JordanGS

+0

Ich habe daran gearbeitet, aber das ist zu viel Arbeit, um atm zu machen, am einfachsten, wenn Sie eine neue Klasse mit dem Namen 'Fraction' erstellen. Siehe hier für [Informationen] (http://stackoverflow.com/questions/8453485/addition-subtraction-multiplication-division-with-fractions-in-java-homewo) – JordanGS

Antwort

1

Ihre Zahlen-Arrays sind Arrays aus ganzen Zahlen, aber Ihr Operatoren-Array ist ein Array von Zeichen. Die einfachste Art, wie ich denke, ist zu

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 
} 
}