2013-04-08 233 views
0

我試圖將我的一維數組轉換爲多維數組[5] [7]。我知道我必須將我的方法sortBySimpleInterest和displayInterest轉換爲接受單個多維數組而不是多個單維數組。將這兩個方法變成一個多維數組時,我將不得不改變計算來接受多維數組?我也很困惑我應該如何使用選擇排序來設置SortbySimpleInterest方法。感謝您的幫助,我是新來的Java將單個二維數組轉換爲多維數組

import java.util.Scanner; 
    public class InterestCalculatorBatchMDA { 
public static void main(String[] args) 
{ 
    int cnt = 0; 
    double[][] arrPrincipalAmt = new double[5][7]; 
    double[][] arrInterestRate = new double[5][7]; 
    double[][] arrTerm = new double[5][7]; 
    double[][] arrSimple = new double[5][7]; 
    double[][] arrCompoundMonthly = new double[5][7]; 
    double[][] arrCompoundDaily = new double[5][7]; 
    double[][] arrCompoundWeekly = new double[5][7]; 


    do{ 
     arrPrincipalAmt[cnt] = getPrincipalAmount(1); 
     arrInterestRate[cnt] = getInterestRate(1); 
     arrTerm[cnt] = getTerm(1); 

     arrSimple[cnt] = round(calculateSimpleInterest(arrPrincipalAmt[cnt], arrInterestRate[cnt], arrTerm[cnt]),5); 
     arrCompoundMonthly[cnt] = round(calculateCompoundInterest(arrPrincipalAmt[cnt], arrInterestRate[cnt],arrTerm[cnt] ,12.0),5); 
     arrCompoundWeekly[cnt] = round(calculateCompoundInterest(arrPrincipalAmt[cnt], arrInterestRate[cnt], arrTerm[cnt], 52.0),5); 
     arrCompoundDaily[cnt] = round(calculateCompoundInterest(arrPrincipalAmt[cnt], arrInterestRate[cnt], arrTerm[cnt], 365.0),5); 

     cnt++; 
    }while (cnt < 5 && askYesNo("Enter another set of data (Yes/No):")); 

    displayInterest(arrPrincipalAmt,arrInterestRate,arrTerm,arrSimple,arrCompoundMonthly,arrCompoundWeekly,arrCompoundDaily,cnt); 
    sortBySimple(arrPrincipalAmt,arrInterestRate,arrTerm,arrSimple,arrCompoundMonthly,arrCompoundWeekly,arrCompoundDaily,cnt); 
    displayInterest(arrPrincipalAmt,arrInterestRate,arrTerm,arrSimple,arrCompoundMonthly,arrCompoundWeekly,arrCompoundDaily,cnt); 

} 


/** Round **/ 
    public static double round(double numb1, double numb2) { 
    double round = ((double) Math.round(numb1*(Math.pow(10, numb2)))/(Math.pow(10, numb2)));; 
    return round; 
    } 

    /** Calculate Simple **/ 
    public static double calculateSimpleInterest(double numb1, double numb2, double numb3) { 
    double calculateSimpleInterest = ((numb1)*(numb2/100.0)*(numb3/12.0)); 
    return calculateSimpleInterest; 
    } 

    /** Calculate Compounded Daily **/ 
    public static double calculateCompoundInterest(double numb1, double numb2, double numb3, double numb4) { 
    double calculateCompoundInterest = (numb1*Math.pow((1.0+((numb2/100.0)/numb4)),(numb4*(numb3/12.0))))-numb1; 
    return calculateCompoundInterest; 
    } 

    /** Get principal amount **/ 
    public static double getPrincipalAmount(double numb1) { 
     Scanner input = new Scanner(System.in); 
     double numb2 = 1; 
    do{System.out.print("Enter Loan Amount: "); 
     numb2 = input.nextDouble(); 
     if(numb2 > 0); 

      else{ 
        System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2); 
      }  
      }while (numb2 < 0); 
    return numb2; 
    } 

    /** Get interest rate **/ 
    public static double getInterestRate(double numb1) { 
     Scanner input = new Scanner(System.in); 
     double numb2=1; 
     do{System.out.print("Enter Yearly Interest Rate (1 to 100 percent): "); 
     numb2 = input.nextDouble(); 
     double getInterestRate = 0; 
    if (numb2 >= 0 && numb2 <= 100) 
     getInterestRate = numb2; 
      else{ 
        System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +numb2); 
      }  
      }while (numb2 <= 0 || numb2 >= 100); 
    return numb2; 
    } 

    /** Get term **/ 
    public static double getTerm(double numb1) { 
     Scanner input = new Scanner(System.in); 
     double numb2=1; 
     do{System.out.print("Enter the Term (in months): "); 
     numb2 = input.nextInt(); 
     double getTerm = 0; 
     if (numb2 > 0) 
     getTerm = numb2; 
      else{ 
        System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2); 
      }  
      }while (numb2 <= 0); 
    return numb2; 
    } 

    /** Sort by simple interest **/ 
    public static void sortBySimple(double[][] arrPrincipalAmt ,double[][] arrInterestRate, double[][] arrTerm, double[][] arrSimple, double[][] arrCompoundMonthly, double[][] arrCompoundWeekly, double[][] arrCompoundDaily, double count){ 
     for(int i = 0;i<count;i++) 
     { 
     for(int j=i+1; j<count;j++) 
     { 
     if(arrSimple[j]<arrSimple[i]) 
     { 
     double temp = arrSimple[i]; 
     arrSimple[i] = arrSimple[j]; 
     arrSimple[j] = temp; 

     double temp1 = arrPrincipalAmt[i]; 
     arrPrincipalAmt[i] = arrPrincipalAmt[j]; 
     arrPrincipalAmt[j] = temp1; 

     double temp2 = arrInterestRate[i]; 
     arrInterestRate[i] = arrInterestRate[j]; 
     arrInterestRate[j] = temp2; 

     double temp3 = arrTerm[i]; 
     arrTerm[i] = arrTerm[j]; 
     arrTerm[j] = temp3; 

     double temp4 = arrSimple[i]; 
     arrSimple[i] = arrSimple[j]; 
     arrSimple[j] = temp4; 

     double temp5 = arrCompoundMonthly[i]; 
     arrCompoundMonthly[i] = arrCompoundMonthly[j]; 
     arrCompoundMonthly[j] = temp5; 

     double temp6 = arrCompoundDaily[i]; 
     arrCompoundDaily[i] = arrCompoundDaily[j]; 
     arrCompoundDaily[j] = temp6; 

     double temp7 = arrCompoundDaily[i]; 
     arrCompoundDaily[i] = arrCompoundDaily[j]; 
     arrCompoundDaily[j] = temp7; 
     } 
     } 
     } 
    } 

    /** Display Interest **/ 
    public static void displayInterest(double[][] amt ,double[][] interest, double[][] term, double[][] simple, double[][] monthly, double[][] weekly, double[][] arrCompoundDaily, int count){ 
    int i=0; 
    System.out.println("[Line #] [Principal Amount] [Interest Rate] [Term] [Simple Interest] [Compound Monthly] [Compound Weekly] [Compound Daily]"); 
    do{ 
    System.out.print((i+1)+"    "); 
    System.out.print(amt[i]+"    "); 
    System.out.print(+interest[i]+"   "); 
    System.out.print(+ term[i]+"   "); 
    System.out.print(+simple[i]+"   "); 
    System.out.print(+monthly[i]+"   "); 
    System.out.print(+weekly[i]+"   "); 
    System.out.println(+arrCompoundDaily[i]); 
    i++; 
    }while(i < count); 
    } 

    /**ask yes or no **/ 
    public static boolean askYesNo(String question) { 
     Scanner input = new Scanner(System.in); 
     String enteredText; 
     boolean isAnswerValid; 

     do{ 
      isAnswerValid = false; 
      System.out.println(question); 
      enteredText = input.nextLine(); 

      if (enteredText.length() > 0) 
      { 
       enteredText = enteredText.toUpperCase(); 

       if(enteredText.equals("YES") || enteredText.equals("Y") || enteredText.equals("NO") || enteredText.equals("N")) 
       { 
        isAnswerValid = true; 
       } 
      } 

      if(isAnswerValid == false) 
      { 
       System.out.println("Please enter 'Yes' or 'No'?"); 
      } 

     } while(isAnswerValid == false); 

     if(enteredText.equals("YES") || enteredText.equals("Y")) 
     { 
      return true; 
     } 

     return false; 
    } 

}

+0

請記住,在Java中,您沒有真正的多維數組,像您在某些其他語言中一樣。相反,你只有數組數組,每個數組只有一個維度。 – 2013-04-08 00:48:44

回答

1

如果你能有一定的計算場景映射到一個單一的結構,那麼簡單的解決辦法是使用一類。

class InterestRateSource { 
    private Double firstFactor; 
    private Double secondFactor; 
    //etc 

    public InterestRateSource(Double firstFactor, Double secondFactor) { 
     this.firstFactor = firstFactor; 
     this.secondFactor = secondFactor; 
    }; 

    public Double getFirstFactor() { 
     return this.firstFactor; 
    }; 

    public Double getSecondFactor() { 
     return this.secondFactor; 
    }; 
}; 
// expand the above to account for all the variables of a single test case. 
// Then simply use a List or whatever suitable collection. 

    List<InterestRateSource> myList = new ArrayList<InterestRateSource>(); 
    myList.add(new InterestRateSource(5, 7)); 
    myList.add(new InterestRateSource(3,4)); 
    for (InterestRateSource currentRate: myList) { 
     // now you are iterating through the list. Do whatever calculations you need to do. 
    };