Java 为什么我的值一直输出1.0?-通用多项式求值器

Java 为什么我的值一直输出1.0?-通用多项式求值器,java,arrays,Java,Arrays,我目前正在为我的实验课编写一个程序,我需要得到输出方面的帮助。 提示是编写计算多项式的通用代码(例如:5x^4+3x^3+2x^2)。说明说我必须使用一个系数数组,其中数组的大小是输入的度数“n”。然后,用户必须输入x值,然后求解每个多项式的值,并将其全部相加 以下是我目前的代码: import java.util.Scanner; public class GenPol { public static void main(String[] args) { Scanner in = n

我目前正在为我的实验课编写一个程序,我需要得到输出方面的帮助。 提示是编写计算多项式的通用代码(例如:5x^4+3x^3+2x^2)。说明说我必须使用一个系数数组,其中数组的大小是输入的度数“n”。然后,用户必须输入x值,然后求解每个多项式的值,并将其全部相加

以下是我目前的代码:

import java.util.Scanner;

public class GenPol {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    String AnsPol = "yes";
    while(AnsPol.equals("yes")) {
        System.out.println("Please enter the largest degree of polynomial");
        int n = in.nextInt();

        if((n>0 || n==0)) {
           double[] x = new double[n+1];
           int arrayLength = x.length;
           for(int degreeLength=0; degreeLength<=arrayLength; degreeLength++){
                double totalVal=0; //overall accumulator
                double indiV =0; //individual accumulator
                for(int i=0; i<=arrayLength; arrayLength--) {
                    System.out.println("Please enter the coefficient for 
                degree " + arrayLength + ":");
                    double coefficient = in.nextDouble();
                    indiV=coefficient;
                }

                System.out.println("Please enter the value for x: ");
                double xVal = in.nextDouble();

                double xPowered = Math.pow(xVal, degreeLength);
                double indivVal = indiV*xPowered;
                x[degreeLength] = indivVal; //store this value into this 
                element
                totalVal += x[degreeLength]; //add the elements together

                String XAns = "yes";
                while(XAns.equals("yes")) {
                    System.out.println("The total value is " + totalVal);

                    System.out.println("Would you like to evaluate another 
                    value of x?");
                    XAns = in.nextLine();
                }

            }
        } else{
            System.out.println("Please enter a degree that's greater than or 
        equal to 0.");
           }
        }
    }
}
有人能给我指出迭代的正确方向吗?我不完全确定为什么我的总值一直输出1.0。我的循环是否正确放置?
非常感谢你

首先,为了让事情更简单,我建议在main方法中创建一个循环。让它调用getInput()、calc()、displayResults()并用这些方法执行这些工作。其次,这里发生了什么
(n>0 | | n==0)
?使用n>=0。第三你的主要问题就在这里

for(int i=0; i<=arrayLength; arrayLength--) {
    System.out.println("Please enter the coefficient for 
    degree " + arrayLength + ":");
    double coefficient = in.nextDouble();
    indiV=coefficient;
}

用于(int i=0;iI已否决此问题,因为没有证据表明在此代码上执行了任何调试。请通过您的问题向我们展示您的调试发现了什么,以及有关特定代码行的特定问题。请参阅:您能否编辑您的问题,简单描述多项式是什么,您的代码应该是什么为此,请提供工作代码的输入和输出示例。还请使用合理的名称重命名您的变量…名称如n(最大多项式度可能是更好的名称)还有,我认为如果你用调试器来处理这个问题,你会很快发现你的错误…如果你不知道如何使用调试器,那么学习…这是非常值得的。我不确定我是否应该因为这确实是正确的而投赞成票,还是因为没有真正回答问题而投反对票…好吧,没有人会去因为他们的代码中有错误,所以我们试图帮助这一个人。上面的评论似乎是想让OP自己做一些工作。我提供一些帮助,看看他是否能自己做到这一点,这比给他答案更有帮助。投票给你的意识好点…A尽管我仍然希望人们能看到这条线索,在这里看到答案,然后分析他们自己的问题,看看是否也在这样做。
for(int i=0; i<=arrayLength; arrayLength--) {
    System.out.println("Please enter the coefficient for 
    degree " + arrayLength + ":");
    double coefficient = in.nextDouble();
    indiV=coefficient;
}