Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 can';t指向数组的第一个对象_Java_Arrays_Static - Fatal编程技术网

Java can';t指向数组的第一个对象

Java can';t指向数组的第一个对象,java,arrays,static,Java,Arrays,Static,我遇到了这个问题,我正在学习java并尝试做一个练习,我不能指向我创建的数组的第一个对象 代码如下: public class prova { public static void main(String[] args) { int y=0; int n=0; int h=20, g=21,d=56,z=44;

我遇到了这个问题,我正在学习java并尝试做一个练习,我不能指向我创建的数组的第一个对象

代码如下:

    public class prova {
        public static void main(String[] args) {

                    int y=0;

                    int n=0;

                    int h=20, g=21,d=56,z=44;

                    int conf=100;

                    riga[] array=new riga[100];

                    array[n]= new riga(3,10,5,6);


                    while(conf !=105) {

                    array[n]= new riga(h++,g++,d++,z++);

                    array[n].nextbet=array[0].importobet;// i want to set the nextbet field of my current array to the importobet value of the first object in this array. Look the output down here.

                    System.out.println("ecco:"+array[n].nextbet);
                    n++;

                    conf++;

                    }
}

class riga {

//variabili

        static public int numerostep;
        static public double importoquota;
        static public double importobet;
        static public boolean esito;
        static public double cassa;
        static public double profitto;
        static public double nextbet;
   //costruttore
        public riga(int a,double d, double c,double f){

                numerostep=a;
                importobet=d;
                importoquota=c;
                cassa=f;


}
  //metodi
        public static void stampaRiga(int a, double b, double c,double f) {

                System.out.println("step:"+a+"***importo:"+b+"***quota:"+c+"***cassa:"+f);
}

}




}
我把问题写在它所在行附近的注释中。在本例中,输出为:

21.0
22.0
23.0 
........
但它应该是:

21.0
21.0
21.0
........

每次迭代都会增加
g
,因此第一次输出是正确的

,这是因为要增加它的初始值h=20 n=0

array[n]= new riga(h++,g++,d++,z++);

1st iteration n=0 h++ ie 20+1=21  so h will have 21
2nd iteration n=1 h++ ie 21+1=22  so h will have 22
3rd iteration n=2 h++ ie 22+1=23  so h will have 23

将g++更改为g+1,您将获得所需的输出。它不是g h++到h+1。如果您对缩进进行一些排序并遵守以下要求,那将非常好:请使用有意义的变量名
array[n]= new riga(h++,g++,d++,z++);

1st iteration n=0 h++ ie 20+1=21  so h will have 21
2nd iteration n=1 h++ ie 21+1=22  so h will have 22
3rd iteration n=2 h++ ie 22+1=23  so h will have 23