Java 尝试将参数(参数)(args[])实现为静态双精度

Java 尝试将参数(参数)(args[])实现为静态双精度,java,parsing,static,command-line-arguments,args,Java,Parsing,Static,Command Line Arguments,Args,我对通过命令行运行参数有点陌生。我在将最后4个参数实现到命令行时遇到问题,因此命令行上的示例输入将是java newton 2 4.005 50 1 2 0 5,其中1 2 0 5是返回捕捉底部静态双精度多项式的系数 它应该是1x^3+2x^2+0^2+5。一切似乎都很好,但我无法让args坚持到底,也不知道为什么。如果有人能帮我的话,我已经花了将近10个小时试图研究,但似乎在这方面找不到任何帮助 import java.util.Scanner; import java.text.Decim

我对通过命令行运行参数有点陌生。我在将最后4个参数实现到命令行时遇到问题,因此命令行上的示例输入将是
java newton 2 4.005 50 1 2 0 5
,其中
1 2 0 5
是返回捕捉底部静态双精度多项式的系数

它应该是
1x^3+2x^2+0^2+5
。一切似乎都很好,但我无法让args坚持到底,也不知道为什么。如果有人能帮我的话,我已经花了将近10个小时试图研究,但似乎在这方面找不到任何帮助

import java.util.Scanner;

import java.text.DecimalFormat;

public class newton {
    public static void main(String[] args) {
        double x0, xnew, xxnew;// Initiating double
        double x1, p1;
        double fx0, fx1;
        double delta, delta1; // amount added to get next iterate
        double error; // error estimate
        double tol = Double.parseDouble(args[2]);// tolerance (max error)
        int i, maxIts, j; // iteration count and maximum number of
                            // iteraterations made
        x0 = Integer.parseInt(args[0]);
        x1 = Integer.parseInt(args[1]);
        p1 = Integer.parseInt(args[4]);
        maxIts = Integer.parseInt(args[3]);

        DecimalFormat fmt = new DecimalFormat("0.############");

        System.out.println("\n");
        System.out.println("Polynomail Root Finder By [Gilbert Jimenez]" + "\n");
        System.out.println("Initial Perameters :" + "\n");
        System.out.println("P0 : = " + args[0]);
        System.out.println("p1 : = " + args[1]);
        System.out.println("Tol = " + tol);
        System.out.println("Maximum = " + maxIts + "\n");
        System.out.println("Polynomial is of order:  4 ");
        System.out.println("Terms of polynomial: " + args[4] + "x^3" + "+" + args[5] + "x^2" + "+" + args[6] + "x" + "+"
                + args[7]);

        {
            // Performing Newton's method
            i = 1;
            error = 100;
            System.out.println("Newtons Method:\t     " + "\n");

            while (i <= maxIts && error > tol) {
                delta = -(f(x0) / fprime(x0));
                error = Math.abs(delta);
                xnew = x0 + delta;

                System.out.println("p" + i + "\t" + fmt.format(xnew));
                i++;
                x0 = xnew;
            }

            System.out.println("\n");
            System.out.println("Solution found after " + i + " " + "itterations :" + fmt.format(x0) + "\n");
        }

        {
            // Performing
            j = 1;
            error = 100;

            System.out.println("Secant Method:\t   " + "\n");
            fx0 = f(x0);
            while (j <= maxIts && error > tol) {
                fx1 = f(x1);
                delta1 = (-fx1 * (x1 - x0) / (fx1 - fx0));
                error = Math.abs(delta1);
                xxnew = x1 + delta1;

                System.out.println("p" + j + "\t" + fmt.format(xxnew));
                j++;
                x0 = x1;
                fx0 = fx1;
                x1 = xxnew;
            }

            System.out.println("\n");
            System.out.println("Solution found after " + j + " " + "itterations :" + fmt.format(x1) + "\n");
        }
    }

    // function of f
    public static double f(double x) {
        return (x * x * x - 2.0 * x * x + 0 * x - 5);
    }

    // derivative of f
    public static double fprime(double x) {
        return (3.0 * x * x - 4.0 * x);
    }

}
import java.util.Scanner;
导入java.text.DecimalFormat;
公共类牛顿{
公共静态void main(字符串[]args){
双精度x0,xnew,xxnew;//初始化双精度
双x1,p1;
双fx0,fx1;
double delta,delta1;//为获得下一次迭代而添加的量
双重错误;//错误估计
double tol=double.parseDouble(args[2]);//容差(最大错误)
int i,maxIts,j;//迭代计数和最大迭代次数
//迭代
x0=整数.parseInt(args[0]);
x1=整数.parseInt(args[1]);
p1=整数.parseInt(args[4]);
maxIts=Integer.parseInt(args[3]);
DecimalFormat fmt=新的DecimalFormat(“0.##################”;
System.out.println(“\n”);
System.out.println(“Gilbert Jimenez编写的多项式根查找器”+“\n”);
System.out.println(“初始参数:“+”\n”);
System.out.println(“P0:=”+args[0]);
System.out.println(“p1:=”+args[1]);
System.out.println(“Tol=”+Tol);
System.out.println(“max=“+maxIts+”\n”);
System.out.println(“多项式的阶数:4”);
多项式的术语:“+args[4]+“x^3”+“+”+args[5]+“x^2”+“+”+args[6]+“x”+“+”
+args[7]);
{
//执行牛顿法
i=1;
误差=100;
System.out.println(“牛顿方法:\t”+“\n”);
而(我){
δ=-(f(x0)/fprime(x0));
误差=Math.abs(增量);
xnew=x0+delta;
System.out.println(“p”+i+“\t”+fmt.format(xnew));
i++;
x0=xnew;
}
System.out.println(“\n”);
System.out.println(“在“+i++”之后找到的解决方案:“+fmt.format(x0)+”\n”);
}
{
//表演
j=1;
误差=100;
System.out.println(“割线方法:\t”+“\n”);
fx0=f(x0);
while(j-tol){
fx1=f(x1);
delta1=(-fx1*(x1-x0)/(fx1-fx0));
错误=Math.abs(delta1);
xxnew=x1+delta1;
System.out.println(“p”+j+“\t”+fmt.format(xxnew));
j++;
x0=x1;
fx0=fx1;
x1=xxx新的;
}
System.out.println(“\n”);
System.out.println(“在“+j+”“+”之后找到的解决方案:“+fmt.format(x1)+”\n”);
}
}
//f的函数
公共静态双f(双x){
返回(x*x*x-2.0*x*x+0*x-5);
}
//f的导数
公共静态双fprime(双x){
返回(3.0*x*x-4.0*x);
}
}

假设多项式的形式为:ax^3+bx^2+c*x+d

更改清单:

  • 将类名更改为Newton(来自Newton)
  • 将多项式系数保存到a、b、c、d中
  • 修改函数f()和fprime()以使用a、b、c、d
请试试看这是否有帮助

   public class Newton {

        static int a = 0;
        static int b = 0;
        static int c = 0;
        static int d = 0;

        public static void main(String[] args)  {

            double x0, xnew, xxnew;// Initiating double
            double x1, p1;
            double fx0, fx1;
            double delta, delta1; // amount added to get next iterate
            double error; // error estimate
            double tol = Double.parseDouble(args[2]);// tolerance (max error)

            int i, maxIts, j; // iteration count and maximum number of
                                // iteraterations made

            x0 = Integer.parseInt(args[0]);
            x1 = Integer.parseInt(args[1]);
            p1 = Integer.parseInt(args[4]);
            maxIts = Integer.parseInt(args[3]);

            DecimalFormat fmt = new DecimalFormat("0.############");

            System.out.println("\n");
            System.out.println("Polynomail Root Finder By [Gilbert Jimenez]" + "\n");
            System.out.println("Initial Perameters :" + "\n");
            System.out.println("P0 : = " + args[0]);
            System.out.println("p1 : = " + args[1]);
            System.out.println("Tol = " + tol);
            System.out.println("Maximum = " + maxIts + "\n");
            System.out.println("Polynomial is of order:  4 ");

            a = Integer.valueOf(args[4]);
            b = Integer.valueOf(args[5]);
            c = Integer.valueOf(args[6]);
            d = Integer.valueOf(args[7]);

            System.out.println("Terms of polynomial: " + a + "x^3" + "+" + b + "x^2" + "+" + c + "x" + "+" + d);

            {

                // Performing Newton's method

                i = 1;
                error = 100;
                System.out.println("Newtons Method:\t     " + "\n");

                while (i <= maxIts && error > tol)

                {

                    delta = -(f(x0) / fprime(x0));
                    error = Math.abs(delta);
                    xnew = x0 + delta;
                    System.out.println("p" + i + "\t" + fmt.format(xnew));
                    i++;
                    x0 = xnew;

                }

                System.out.println("\n");
                System.out.println("Solution found after " + i + " " + "itterations :" + fmt.format(x0) + "\n");

            }

            {

                // Performing

                j = 1;
                error = 100;
                System.out.println("Secant Method:\t   " + "\n");

                fx0 = f(x0);

                while (j <= maxIts && error > tol)

                {

                    fx1 = f(x1);
                    delta1 = (-fx1 * (x1 - x0) / (fx1 - fx0));
                    error = Math.abs(delta1);
                    xxnew = x1 + delta1;
                    System.out.println("p" + j + "\t" + fmt.format(xxnew));

                    j++;
                    x0 = x1;
                    fx0 = fx1;
                    x1 = xxnew;

                }

                System.out.println("\n");
                System.out.println("Solution found after " + j + " " + "itterations :" + fmt.format(x1) + "\n");

            }

        }

        // function of f

        public static double f(double x)

        {
            return (a * x * x * x + b * x * x + c * x + d);
        }

        // derivative of f

        public static double fprime(double x)

        {
            return (3 * a * x * x + 2 * b * x + c);
        }

    }
公共类{
静态int a=0;
静态int b=0;
静态int c=0;
静态int d=0;
公共静态void main(字符串[]args){
双精度x0,xnew,xxnew;//初始化双精度
双x1,p1;
双fx0,fx1;
double delta,delta1;//为获得下一次迭代而添加的量
双重错误;//错误估计
double tol=double.parseDouble(args[2]);//容差(最大错误)
int i,maxIts,j;//迭代计数和最大迭代次数
//迭代
x0=整数.parseInt(args[0]);
x1=整数.parseInt(args[1]);
p1=整数.parseInt(args[4]);
maxIts=Integer.parseInt(args[3]);
DecimalFormat fmt=新的DecimalFormat(“0.##################”;
System.out.println(“\n”);
System.out.println(“Gilbert Jimenez编写的多项式根查找器”+“\n”);
System.out.println(“初始参数:“+”\n”);
System.out.println(“P0:=”+args[0]);
System.out.println(“p1:=”+args[1]);
System.out.println(“Tol=”+Tol);
System.out.println(“max=“+maxIts+”\n”);
System.out.println(“多项式的阶数:4”);
a=整数.valueOf(args[4]);
b=整数.valueOf(args[5]);
c=整数.valueOf(args[6]);
d=整数.valueOf(args[7]);
System.out.println(“多项式项:”+a+“x^3”+“+”+b+“x^2”+“+”+c+“x”+“+”+d);
{
//执行牛顿法
i=1;
误差=100;
System.out.println(“牛顿方法:\t”+“\n”);
而(我)
{
δ=-(f(x0)/fprime(x0));
误差=Math.abs(增量);
xnew=x0+delta;
System.out.println(“p”+i+“\t”+fmt.format(xnew));