0

我已经尝试解决这个问题几个小时:为什么当 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.0i; 2.0i
1.0+-2.0i; 1.0+-2.0i
1.0+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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
4

2 回答 2

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);
    }
}
于 2013-11-01T05:07:56.290 回答
0

为什么当 l 循环还没有完成时 j 和其他变量会发生变化? 这是因为每次我增加时都会初始化 j,k 和 l。

在这

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

这里 when i=-a then j=-b,k=-c and l=-d now when then i 递增 theni=-a then j=-b,k=-c and l=-d

于 2013-11-01T05:04:47.363 回答