简体   繁体   English

将一维数组转换为多维数组

[英]converting single dimensional array into multidimensional array

I'm trying to convert my single dimensional array into a multidimensional array. 我正在尝试将我的一维数组转换为多维数组。 Convert the existing Interest Calculator Batch application from several single dimensional arrays into 1 multi-dimensional array. 将现有的兴趣计算器批处理应用程序从几个单维数组转换为1个多维数组。 Use the following data type: double[][] values; 使用以下数据类型:double [] [] values; Hint: The maximum number of data sets(5) will be your row and the each data array (from the original InterestCalculatorBatch)(7)will be a column. 提示:数据集的最大数量(5)将是您的行,每个数据数组(来自原始的InterestCalculatorBatch)(7)将是一列。 2.All data will be contained within a single multi-dimensional array. 2.所有数据将包含在一个多维数组中。 3. Modify the sortBySimple and displayInterest methods to accept a single multi-dimensional array instead of multiple single dimensional arrays 3.修改sortBySimple和displayInterest方法以接受单个多维数组,而不是多个单个多维数组

I fixed the displayInterest to accept a single multi dimensional array. 我修复了displayInterest以接受单个多维数组的问题。 I am just confused if I have the sortBySimple set up correctly and when the program calculates the interest if I need to fix those or keep them as is. 如果我正确设置了sortBySimple,并且程序在计算利息时需要解决问题或保持现状,我只会感到困惑。 Thank you for the help. 感谢您的帮助。

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[i][j] < arrSimple[i][j])
      {
      double temp = arrSimple[i][j];
      arrSimple[i][j] = arrSimple[i][j];
      arrSimple[i][j] = temp;

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

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

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

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

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

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

      double temp7 = arrCompoundWeekly[i][j];
      arrCompoundWeekly[i][j] = arrCompoundWeekly[i][j];
      arrCompoundWeekly[i][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][i]+"                ");
    System.out.print(+interest[i][i]+"           ");
    System.out.print(+ term[i][i]+"           ");
    System.out.print(+simple[i][i]+"          ");
    System.out.print(+monthly[i][i]+"           ");
    System.out.print(+weekly[i][i]+"           ");
    System.out.println(+arrCompoundDaily[i][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;
  }

}

Your code looks like an attempt to write C in Java. 您的代码看起来像是尝试用Java编写C。 Java is an OO language, and you will have much more success if you design and write your programs in to make use of that. Java是一种OO语言,如果您设计和编写程序来利用它,您将获得更大的成功。 Data structures that are "smeared" across multiple arrays like that are fundamentally awkward to deal with. 像这样在多个数组上“涂抹”的数据结构从根本上很难处理。

The first thing you need to do to remedy this is to create a class that represents an entry in the arrays. 解决此问题的第一件事是创建一个表示数组中条目的类。

Trying to fix the program while retaining the current "smeared" data structure design is ... to be blunt ... a waste of time. 试图在保留当前“模糊”数据结构设计的同时修复程序,直言不讳……浪费时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM