Java:如何使这个微积分方法更有效率?

时间:2015-02-05 02:33:38

标签: java calculus

如何更轻松地初始化所有变量?

有一个微积分包吗?

整体上是否有更有效的解决方案?

import java.util.Scanner;

public class limits {

public static void main(String[] args)
{
    String introMessage = "          ***Calculus: Limits***"                 + "\n"
            +             "This application uses the method of exhaustion"   + "\n"
            +             "to test limits. You enter the number that x"      + "\n"
            +             "approches and this program will give you three"   + "\n"
            +             "numbers on either side of the limit showing"      + "\n"
            +             "closer approximations of the limit.";
    System.out.println(introMessage);
    System.out.println();
    String polynomialMessage = "Our function: "                 + "\n"
                            +  "  lim   f(x) = x^2 + x + 1 = L" + "\n"
                            +  "x -> a";
    System.out.println(polynomialMessage);
    System.out.println();
    System.out.println("As x approaches a, what will our limit L be?");
    System.out.println();

我可以一次初始化所有这些吗?

    // initialize variables
    double belowAOne = 0.0;
    double belowATwo = 0.0;
    double belowAThree = 0.0;
    double belowAFour = 0.0;
    double aboveAOne = 0.0;
    double aboveATwo = 0.0;
    double aboveAThree = 0.0;
    double aboveAFour = 0.0;

    double totalBAOne = 0.0;
    double totalBATwo = 0.0;
    double totalBAThree = 0.0;
    double totalBAFour = 0.0;
    double totalAAOne = 0.0;
    double totalAATwo = 0.0;
    double totalAAThree = 0.0;
    double totalAAFour = 0.0;

    double L = 0;


    // create a Scanner object named sc
    Scanner sc = new Scanner(System.in);



    // perform invoice calculations until choice isn't equal to "y" or "Y"
    String choice = "y";
    while (!choice.equalsIgnoreCase("n"))
    {
      System.out.println("Please enter a whole number between 1 and 10 for a: ");
      double a = sc.nextDouble();


      if (a > 0 && a <=10 )
      {
      // calculate L
      L = (a*a) + a + 1;

      // create values that approaches a
      belowAOne = a - .5;
      belowATwo = a - .1;
      belowAThree = a - .01;
      belowAFour = a - .001;
      aboveAOne = a + .5;
      aboveATwo = a + .1;
      aboveAThree = a + .01;
      aboveAFour = a + .001;

      totalBAOne = (belowAOne * belowAOne) + belowAOne + 1;
      totalBATwo = (belowATwo * belowATwo) + belowATwo + 1;
      totalBAThree = (belowAThree * belowAThree) + belowAThree + 1;
      totalBAFour = (belowAFour * belowAFour) + belowAFour + 1;
      totalAAOne = (aboveAOne * aboveAOne) + aboveAOne + 1;
      totalAATwo = (aboveATwo * aboveATwo) + aboveATwo + 1;
      totalAAThree = (aboveAThree * aboveAThree) + aboveAThree + 1;
      totalAAFour = (aboveAFour * aboveAFour) + aboveAFour + 1;

      String chart = "      x     " +   "x^2 + x + 1"    + "\n"
              +         "---------+--------------"       + "\n"
              + "     " + belowAOne +   "   :   " + totalBAOne   + "\n"
              + "     " + belowATwo +   "   :   " + totalBATwo   + "\n"
              + "     " + belowAThree + "  :   " + totalBAThree  + "\n"
              + "     " + belowAFour  + " :   " + totalBAFour    + "\n"
              + "     " + " a " +       "   :   " + "L"          + "\n"
              + "     " + aboveAFour +  " :   " + totalAAFour    + "\n"
              + "     " + aboveAThree + "  :   " + totalAAThree  + "\n"
              + "     " + aboveATwo   + "   :   " + totalAATwo   + "\n"
              + "     " + aboveAOne  +  "   :   " + totalAAOne   + "\n";
      System.out.println();
      System.out.println();
      System.out.println();
      System.out.println(chart);
      System.out.println();
      System.out.println("As X approaches " + a + ", our guess "
              +          "for L is: " + L);
      System.out.println();

      // end the program
      choice = "n";
      }

      else
      {
       System.out.println();
       System.out.println("***Invalid Entry***");
       System.out.println();   
      }
    }
} 

}

有没有更好的方法来编写这个程序?

1 个答案:

答案 0 :(得分:0)

或者您可以像这样使用循环:

double[] belowA = {a - .5, a - .1, a - .01, a - .001};
double[] totalBA = new double[4];
for(int i = 0; i < 4; ++i) {
    totalBA[i] = (belowA[i] * belowA[i]) + belowA[i] + 1;
}

也使用StringBuilder / StringBuffer

最后你可以创建一个方法,以相同的方式处理下面/上面没有重复的代码