【问题标题】:Java: How can I make this calculus method on limits more efficient?Java:我怎样才能使这种关于极限的微积分方法更有效?
【发布时间】:2015-02-05 02:33:38
【问题描述】:

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

有微积分包吗?

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

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();   
      }
    }
} 

}

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

【问题讨论】:

    标签: java calculus


    【解决方案1】:

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

    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

    最后你可以创建一个方法来以相同的方式处理下面/上面而不需要重复的代码

    【讨论】:

      猜你喜欢
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      相关资源
      最近更新 更多