需要数组,但找到int

时间:2019-03-20 11:13:22

标签: java

我的节目有什么问题?

public class Square{
  public int x;
  public Square() {
    int x[] = new int[10];
    int y;
    x[0] = 7;
  }

  public void root() {
    for (int i = 0; i < 10; i++) {
        x[i+1] = x[i] - (Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5));
        System.out.println(x[i + 1]);
    }
  }
}

我没有发现问题所在,我的for循环似乎无法正常工作,由于某种原因,它一直在显示错误。有人可以帮我解决这个问题吗?

好的,我现在写了这个程序:

    public class Square
    {
       public double x[];
       public void root()
       {
    double x[] = new double[10];
    x[0]=7;
    for(int i=0; i<8; i++)
    {
        x[i+1]=x[i]-(Math.pow(x[i]-2.5,2))/(2*(x[i]-2.5));
        System.out.println(x[i+1]);
    }
}
}

它正在显示以下输出:

3.625
3.0625
2.78125
2.640625
2.5703125
2.53515625
2.517578125
   java.lang.NullPointerException
    at Square.root(Square.java:14)
java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 at Square.root(Square.java:11)

我不知道为什么会出现这些错误。同样,在某些时候答案应该是6.25。但是,它不会显示该输出。

6 个答案:

答案 0 :(得分:2)

这是因为您之前将x定义为int,而不是整数数组。

尝试一下:

public class Square {
    public int x[];

    public Square() {
        this.x = new int[10];
        int y;
        x[0] = 7;
    }

    public void root() {
        for(int i = 0; i < 10; i++) {
            x[i + 1] = x[i] - (Math.pow(x[i] - 2.5, 2)) / (2 * (x[i]  -2.5));
            System.out.println(x[i + 1]);
        }
    }
}

答案 1 :(得分:1)

您的构造函数有一个局部变量int[] x,该变量在构造函数的末尾不可用。

尝试一下:

public class Square{
  // initialize to array of ten ints
  public int x[] = new int[10];

  public Square() {
    x[0] = 7;
  }

  public void root() {
    for (int i = 0; i < 10; i++) {
        x[i+1] = x[i] - (Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5));
        System.out.println(x[i + 1]);
    }
  }
}

编辑int y对于构造函数是本地的,在构造函数范围的末尾也将其丢弃。

答案 2 :(得分:0)

以下是注释代码,解释了该问题:

public class Square
{
  // Here, x is defined as an attribute of class Square, of type int
  public int x;
  public Square()
  {
    // Here, x is locally defined as a local variable, of type int[]
    // It shadows the attribute x. This is considered as a bad practice.
    int x[] = new int[10];
    int y;
    // Here, x is the local variable, of type int[]. It IS an array so
    // this line is valid.
    x[0]=7;
  }// From this point, the local variable x is not defined anymore
   // (that is the point of a local variable)

现在在这里:

  public void root()
  {
    for(int i=0; i<10; i++)
    {
      // Here, x is referencing the attribute of class Square, which is an int
      // But you try to access it as if it was and int[]
      x[i+1]

答案 3 :(得分:0)

首先,您必须将x声明为数组:

public int[] x;

请注意, java样式不是int x[];
然后在Square()内部,您必须像这样初始化x

x = new int[10];

最后,这个:

(Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5))

返回一个double,因此您必须将其强制转换为int

x[i+1] = x[i] - (int) ((Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5)));

因此您的代码应为:

public int[] x;
public void Square() {
    x = new int[10];
    x[0] = 7;
}
public void root() {
    if (x == null)
        Square();
    for(int i = 0; i < x.length - 1; i++) {
        x[i+1] = x[i] - (int) ((Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5)));
        System.out.println(x[i+1]);
    }
}

在循环内,您正在访问i + 1项,因此循环的计数器必须采用最多x.length - 2的值,这就是为什么我在代码中使用i < x.length - 1
我已经从y中删除了Square()的声明,因为它没有被使用。

答案 4 :(得分:0)

public class Square
{
   public double x[];
   public Square()
  {
    this.x = new double[10];
    x[0]=7;
  }
  public void root()
  {
    System.out.println(x[0]);
    for(int i=1; i<10; i++)
    {
        x[i]=x[i-1]-(Math.pow(x[i-1]-2.5,2))/(2*(x[i-1]-2.5));
        System.out.println(x[i]);
    }
  }
}

答案 5 :(得分:0)

尝试使用此代码

public class Square
{
   public double x[];
   public void root()
   {
        x = new double[10];
        x[0]=7;
        for(int i=1; i<10; i++)
        {
            x[i]=x[i-1]-(Math.pow(x[i-1]-2.5,2))/(2*(x[i-1]-2.5));
            System.out.println(x[i]);
        }
    }
}