为什么我的for循环在java中没有正确递增?

时间:2013-11-01 05:00:11

标签: java for-loop

我一直试图解决这个问题好几个小时:为什么当l循环还没有完成时j和其他变量会改变?这是我的代码:

public class main
{

        public static void main (String[] args)
        {

        String name=args[0];
        double a=Double.parseDouble(args[1]);
        double b=Double.parseDouble(args[2]);
        double c=Double.parseDouble(args[3]);
        double d=Double.parseDouble(args[4]);

        //for complexes
        for (double i = -a; i<=a; i++){
            for (double j = -b; j<=b; j++){
                for (double k = -c; k<=c; k++){
                    for (double l = -d; l<=d; l++){
                        complex c1 = new complex (i, j);
                        complex c2 = new complex (k, l);
                        System.out.println(c1+"; "+c2);

                    }

                }
            }
        }
    }
}

//And the complex class:


public class complex {
    private static double c;
    private static double n;

    public complex(double c, double n){
        this.c=c;
        this.n=n;
    }
    public String toString(){
        if ((n==0)&&(c==0))
            return "0";
        if (n==0)
            return ""+c;
        if (c==0)
            return ""+n+"i";
        else return ""+c+"+"+n+"i";
    }
}

输出结果为:

-1.0+-2.0i; -1.0+-2.0i
-1.0+-1.0i; -1.0+-1.0i
-1.0; -1.0
-1.0+1.0i; -1.0+1.0i
-1.0+2.0i; -1.0+2.0i
-2.0i; -2.0i
-1.0i; -1.0i
0; 0
1.0i; 1.0i
ect.

但输出应该是

-1.0-2.0i; -1.0-2.0i
-1.0-2.0i; -1.0-1.0i
-1.0-2.0i; -1.0
-1.0-2.0i; -1.0+1.0i
-1.0-2.0i; -1.0+2.0i
-1.0-2.0i; -2.0i
ect.

但复杂课程中的任何内容都无关紧要,对吧?因为即使我使用了相同名称的变量,也不应该从他们的课外访问,对吧? 非常感谢您的帮助!我也问过其他人,也没有人能够发现我的错误,所以非常感谢。

编辑:

之前我没有发布完整的输出。这是我得到的输出:

-1.0+-2.0i; -1.0+-2.0i
-1.0+-1.0i; -1.0+-1.0i
-1.0; -1.0
-1.0+1.0i; -1.0+1.0i
-1.0+2.0i; -1.0+2.0i
-2.0i; -2.0i
-1.0i; -1.0i
0; 0
1.0i; 1.0i
2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1.0i
1.0; -1.0+2.0i
1.0; -2.0i
1.0; -1.0i
1.0; 0
1.0; 1.0i
1.0; 2.0i
1.0; 1.0-2.0i
1.0; 1.0-1.0i
1.0; 1.0
1.0; 1.0+1.0i
1.0; 1.0+2.0i
1.0+1.0i; -1.0-2.0i
1.0+1.0i; -1.0-1.0i
1.0+1.0i; -1.0
1.0+1.0i; -1.0+1.0i
1.0+1.0i; -1.0+2.0i
1.0+1.0i; -2.0i
1.0+1.0i; -1.0i
1.0+1.0i; 0
1.0+1.0i; 1.0i
1.0+1.0i; 2.0i
1.0+1.0i; 1.0-2.0i
1.0+1.0i; 1.0-1.0i
1.0+1.0i; 1.0
1.0+1.0i; 1.0+1.0i
1.0+1.0i; 1.0+2.0i
1.0+2.0i; -1.0-2.0i
1.0+2.0i; -1.0-1.0i
1.0+2.0i; -1.0
1.0+2.0i; -1.0+1.0i
1.0+2.0i; -1.0+2.0i
1.0+2.0i; -2.0i
1.0+2.0i; -1.0i
1.0+2.0i; 0
1.0+2.0i; 1.0i
1.0+2.0i; 2.0i
1.0+2.0i; 1.0-2.0i
1.0+2.0i; 1.0-1.0i
1.0+2.0i; 1.0
1.0+2.0i; 1.0+1.0i
1.0+2.0i; 1.0+2.0i

2 个答案:

答案 0 :(得分:0)

为什么j和其他变量在l循环尚未完成时会发生变化? 因为j,k和l每当我增加时都会被初始化。

在此

for (double j = -b; j<=b; j++){
            for (double k = -c; k<=c; k++){
                for (double l = -d; l<=d; l++){

此处i=-a then j=-b,k=-c and l=-d 现在当i增加时,i=-a then j=-b,k=-c and l=-d

答案 1 :(得分:0)

我有一种感觉,你是在追求这种行为:

for (double i = -a; i<=a; i++){
    for (double j = -b; j<=b; j++){
        complex c1 = new complex (i, j);
        System.out.println(c1);
    }
}
for (double k = -c; k<=c; k++){
    for (double l = -d; l<=d; l++){
        complex c2 = new complex (k, l);
        System.out.println(c2);
    }
}