Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何使极限上的微积分方法更有效?_Java_Calculus - Fatal编程技术网

Java:如何使极限上的微积分方法更有效?

Java:如何使极限上的微积分方法更有效?,java,calculus,Java,Calculus,如何更轻松地初始化所有变量 有微积分软件包吗 总体而言,是否有更有效的解决方案 import java.util.Scanner; public class limits { public static void main(String[] args) { String introMessage = " ***Calculus: Limits***" + "\n" + "This app

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

有微积分软件包吗

总体而言,是否有更有效的解决方案

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();   
      }
    }
} 
//初始化变量
双贝洛瓦一=0.0;
双belowATwo=0.0;
双贝洛瓦三=0.0;
双贝洛瓦福=0.0;
二次以上Aone=0.0;
两倍于WO=0.0;
树上双倍=0.0;
四倍以上=0.0;
双倍总能量=0.0;
双倍总Batwo=0.0;
双倍totalBAThree=0.0;
双总Bafour=0.0;
双倍总AaOne=0.0;
双总AAtwo=0.0;
两倍总AAThree=0.0;
双总AAfour=0.0;
双L=0;
//创建名为sc的扫描仪对象
扫描仪sc=新的扫描仪(System.in);
//执行发票计算,直到选项不等于“y”或“y”
字符串选择=“y”;
而(!choice.equalsIgnoreCase(“n”))
{
System.out.println(“请输入一个介于1和10之间的整数作为:”;
双a=sc.nextDouble();

如果(a>0&&a或,您可以使用如下循环:

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;
}
double[]belowA={a-.5,a-.1,a-.01,a-.001};
double[]totalBA=新的双精度[4];
对于(int i=0;i<4;++i){
totalBA[i]=(贝洛瓦[i]*贝洛瓦[i])+贝洛瓦[i]+1;
}
还使用StringBuilder/StringBuffer

最后,您可以创建一个方法,以相同的方式处理上面/下面的代码,而无需重复代码