Java 需要数组,但找到int
我的程序怎么了Java 需要数组,但找到int,java,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.
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]);
}
}
}
我不知道为什么会出现这些错误。此外,在某一点上,答案应该是6.25。但是,它没有显示输出。这是因为您以前将
x
定义为一个int
,而不是一个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]);
}
}
}
公共类广场{
公共int x[];
公众广场(){
这个.x=新的整数[10];
int-y;
x[0]=7;
}
公共void root(){
对于(int i=0;i<10;i++){
x[i+1]=x[i]-(数学功率(x[i]-2.5,2))/(2*(x[i]-2.5));
System.out.println(x[i+1]);
}
}
}
您的构造函数有一个局部变量int[]x
,它在构造函数的末尾被打乱了顺序
试试这个:
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]);
}
}
}
公共类广场{
//初始化为10个整数的数组
公共整数x[]=新整数[10];
公众广场(){
x[0]=7;
}
公共void root(){
对于(int i=0;i<10;i++){
x[i+1]=x[i]-(数学功率(x[i]-2.5,2))/(2*(x[i]-2.5));
System.out.println(x[i+1]);
}
}
}
编辑:构造函数的
int y
是本地的,它也会在构造函数作用域的末尾被丢弃。下面是解释问题的注释代码:
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]
public void root()
{
对于(int i=0;i首先必须将x
声明为数组:
public int[] x;
请注意,java样式不是intx[];
然后在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]);
}
}
public int[]x;
公众休憩广场({
x=新整数[10];
x[0]=7;
}
公共void root(){
如果(x==null)
正方形();
对于(int i=0;i
在循环内部,您正在访问i+1
项,因此循环计数器的值必须达到x.length-2
,这就是为什么我在代码中有:i
我已从Square()
中删除了y
的声明,因为它未被使用。公共类Square
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]);
}
}
}
{
公共双x[];
公众广场()
{
这个.x=新的双精度[10];
x[0]=7;
}
公共void root()
{
System.out.println(x[0]);
对于(inti=1;i请尝试使用此代码
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]);
}
}
}
公共类广场
{
公共双x[];
公共void root()
{
x=新双精度[10];
x[0]=7;
for(int i=1;iVariable scope是正在发生的事情。您有两个名为x的不相关变量,在不同的作用域中具有不同的类型(int和int[])。如何才能System.out.println(x[10])?@YaroslavNudnenko我意识到了,我纠正了。第二个代码有什么问题?@PravyaAgarwal你是什么意思?第二个代码是什么?我的意思是我在问题中写了另一个代码。你能帮我找出问题所在吗?好的,我已经编辑了程序,但没有得到任何输出。请帮助。Int y没有被使用。哦,哦!请检查一下e现在提出问题。@PravyaAgarwal问题是您尚未初始化x数组的值。系统知道您有一个长度为10的x数组,但不知道数组中有什么。请尝试初始化该数组。
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]);
}
}
}