Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.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 计算Sierpinski'中三角形的数量;s三角_Java - Fatal编程技术网

Java 计算Sierpinski'中三角形的数量;s三角

Java 计算Sierpinski'中三角形的数量;s三角,java,Java,我应该修改这个Sierpinski的三角形程序来计算三角形的数量。所以我试着每次做三角形时增加计数,但不知怎么的,我的计数并没有增加 public class SierpinskiTriangle extends Applet { public int SeirpTri(Graphics g, int x1, int y1, int x2, int y2, int x3, int y3, int n, int count) { this.setBack

我应该修改这个Sierpinski的三角形程序来计算三角形的数量。所以我试着每次做三角形时增加计数,但不知怎么的,我的计数并没有增加

public class SierpinskiTriangle extends Applet 
{  

    public int SeirpTri(Graphics g, int x1, int y1, int x2, int y2, int x3, int y3, int n, int count)  
    {
        this.setBackground(new Color(0,0,0));
        this.setSize(700, 500);
        if ( n == 0 )
        {
            g.setColor(new Color(0, 255, 0));
            g.drawLine(x1, y1, x2, y2);     // if n = 0 draw the triangle
            g.drawLine(x2, y2, x3, y3);
            g.drawLine(x3, y3, x1, y1);        
            return 1;     
        }

        int xa, ya, xb, yb, xc, yc;   // make 3 new triangles by connecting the midpoints of
        xa = (x1 + x2) / 2;             //. the previous triangle 
        ya = (y1 + y2) / 2;
        xb = (x1 + x3) / 2;
        yb = (y1 + y3) / 2;
        xc = (x2 + x3) / 2;
        yc = (y2 + y3) / 2; 
        SeirpTri(g, x1, y1, xa, ya, xb, yb, n - 1, count++);   // recursively call the function using the 3 triangles
        SeirpTri(g, xa, ya, x2, y2, xc, yc, n - 1, count++);
        SeirpTri(g, xb, yb, xc, yc, x3, y3, n - 1, count++);
        return count;
    }

    public void paint(Graphics g)    
    {
        int recursions = 3;
        int count=1;
        // call the recursive function sending in the number of recursions
        SeirpTri(g, 319, 0, 0, 479, 639, 479, recursions, count);

        // Counting triangles using math algorithm;
        int count2 = 1;
        if (recursions ==0) {
            count2 =1;
        }
        else {
            count2 = (int) Math.pow(3,(recursions-1)) * 3;
        }
        System.out.println("Correct answer is: " +count2);
        System.out.println("Answer using recurvise is: " +count*3);
    }        
}

返回
count
,但从不查看调用
SeirpTri
的结果

而不是:

SeirpTri(g, x1, y1, xa, ya, xb, yb, n - 1, count++);   // recursively call the function using the 3 triangles
SeirpTri(g, xa, ya, x2, y2, xc, yc, n - 1, count++);
SeirpTri(g, xb, yb, xc, yc, x3, y3, n - 1, count++);
return count;
尝试以下方法:

return
    SeirpTri(g, x1, y1, xa, ya, xb, yb, n - 1)
    + SeirpTri(g, xa, ya, x2, y2, xc, yc, n - 1)
    + SeirpTri(g, xb, yb, xc, yc, x3, y3, n - 1);

您根本不需要count参数。每个
SeirpTri
调用只需要知道它及其“子项”(在调用树上)创建的三角形。“root”调用(在
paint
中)将返回总计。

您返回
count
,但从不查看调用
SeirpTri
的结果

而不是:

SeirpTri(g, x1, y1, xa, ya, xb, yb, n - 1, count++);   // recursively call the function using the 3 triangles
SeirpTri(g, xa, ya, x2, y2, xc, yc, n - 1, count++);
SeirpTri(g, xb, yb, xc, yc, x3, y3, n - 1, count++);
return count;
尝试以下方法:

return
    SeirpTri(g, x1, y1, xa, ya, xb, yb, n - 1)
    + SeirpTri(g, xa, ya, x2, y2, xc, yc, n - 1)
    + SeirpTri(g, xb, yb, xc, yc, x3, y3, n - 1);

您根本不需要count参数。每个
SeirpTri
调用只需要知道它及其“子项”(在调用树上)创建的三角形。“root”调用(在
paint
中)将返回总计。

在Java中,每个参数都通过值传递。这意味着对
count
的任何更改将仅对方法进行局部更改,而不会更改从父方法传递到方法中的
count
对象。
您可以(并且您的代码已经)通过返回
count
param来绕过此问题。您只需在父方法中设置
count

更换每一行:

SeirpTri(...);
与:


在Java中,每个参数都通过值传递。这意味着对
count
的任何更改将仅对方法进行局部更改,而不会更改从父方法传递到方法中的
count
对象。
您可以(并且您的代码已经)通过返回
count
param来绕过此问题。您只需在父方法中设置
count

更换每一行:

SeirpTri(...);
与:


答案不是“无限”吗?:-)使用全局静态变量跟踪计数可能更容易。答案不是“无穷大”吗?:-)使用全局静态变量跟踪计数可能更容易。