Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.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/3/arrays/14.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_Arrays_If Statement_For Loop_Intellij Idea - Fatal编程技术网

用Java打印出一个数组

用Java打印出一个数组,java,arrays,if-statement,for-loop,intellij-idea,Java,Arrays,If Statement,For Loop,Intellij Idea,我写了这个程序: public class FunctionEvaluator { public static Scanner console = new Scanner(System.in); public static void main(String[] args) { int degree; System.out.print("What degree would you like your polynomial to be? ");

我写了这个程序:

public class FunctionEvaluator {
    public static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {
        int degree;
        System.out.print("What degree would you like your polynomial to be? ");
        degree = console.nextInt();
        int a[] = new int[degree + 1];
        int coefficient;

        for (int i = 0; i <= degree; i++) {
            System.out.print("Coefficient of the x^" + (degree - i) + " term: ");
            coefficient = console.nextInt();

            a[i] = coefficient;
        }

        System.out.print("f(x) = ");

        for (int i = 0; i < degree + 1; i++) {
            System.out.print(a[i] + "x^" + (degree - i));

            if (a[i] == degree) {
                System.out.println(" ");
            } else if (a[i + 1] >= 0 && a[i + 1] < degree) {
                System.out.print(" + ");
            } else if (a[i] < 0) {
                    System.out.print(" - ");
            } else {
                    System.out.print(" ");
            }
        }

        System.out.println();

        int x;
        int yN = 0;
        double fOfX = 0;
        double sum1;

        do {
            System.out.print("Give a value for x: ");
            x = console.nextInt();
            int deg = degree;
            for (int i = 0; i <= degree; i++) {
                sum1 = a[i] * Math.pow(x, deg);
                deg--;
                fOfX = fOfX + sum1;
            }

            System.out.println("f(" + x + ") = " + fOfX);

            System.out.print("Do you want to go again (1 for yes and 0 for no)? ");
            yN = console.nextInt();
        } while (yN == 1);

        System.out.println("Done.");

    }
公共类FunctionEvaluator{
公共静态扫描仪控制台=新扫描仪(System.in);
公共静态void main(字符串[]args){
智力度;
System.out.print(“您希望多项式的阶数是多少?”);
度=console.nextInt();
int a[]=新int[度+1];
int系数;
对于(int i=0;i=0&&a[i+1]<度){
系统输出打印(“+”);
}else如果(a[i]<0){
系统输出打印(“-”);
}否则{
系统输出打印(“”);
}
}
System.out.println();
int x;
int-yN=0;
双fOfX=0;
双sum1;
做{
System.out.print(“为x指定一个值:”);
x=console.nextInt();
int deg=度;
对于(int i=0;i
else if(a[i+1]>=0&&a[i+1]<度)

这似乎是您的问题。您将使用数组+1的大小。我猜您的错误是ArrayIndexOutOfBoundsException

您正在索引
a[i+1]
,但a是int[degree+1],因此在循环结束时,您试图达到[degree+1],而没有这样的项,最后一个是[degree]

您可能需要:

} else if (i < degree && a[i + 1] >= 0 && a[i + 1] < degree) {
您将[i]与学位进行比较,但它与学位无关。您可能希望比较
i==degree
。请参见此示例:

degree = 2
a[0] = 7, a[1] = 2, a[2] = 3 // 7 * x^2 + 2 * x + 3
正如您所看到的,应该将度与索引进行比较,而不是与数组项的值进行比较

我建议您在重写代码时牢记以下技巧:尝试使用数组中的索引,而不是“相反”。这将更自然,每个索引都将精确地表示指数:

a[2] = 7, a[1] = 2, a[0] = 3 // note: 3 * x^0 = 3 * 1 = 3

因为您无论如何都要填充数组中的所有元素,所以按降序循环也没关系。

它崩溃的异常是什么?我猜是ArrayIndexOutOfBoundsException.a[i]是您能达到的最高值。
程序崩溃
->stacktrace,并在运行时查看它。它会告诉您发生了什么wrong@Gavriel在第二段的第一个else-if语句中code@ostrichofevil线程“main”中出现异常java.lang.ArrayIndexOutOfBoundsException:5是的,错误是ArrayIndexOutOfBoundsException:5您必须重新考虑循环的执行方式。每当您在数组中使用+1或-1时,您必须采取额外的预防措施,以确保您没有ArrayIndexOutOfBoundsException。好的,谢谢。我将尝试重新编写它为了避免错误。这就是我的想法,但我找不到另一种方法来打印函数,因为我在数组中对系数进行了向后排序,也就是说,如果是四次函数,x^4的系数在点0,x^3在点1,依此类推。然后在条件的开头加上I<度-&即可
if (a[i] == degree) {
degree = 2
a[0] = 7, a[1] = 2, a[2] = 3 // 7 * x^2 + 2 * x + 3
a[2] = 7, a[1] = 2, a[0] = 3 // note: 3 * x^0 = 3 * 1 = 3