【问题标题】:Java: How do a make a variable outside of the loop that remembers the int made in a loop?Java:如何在循环外创建一个变量来记住循环中创建的 int?
【发布时间】:2020-06-22 15:57:36
【问题描述】:

提示要求我修改我编写的代码以生成谢尔宾斯基三角形以制作谢尔宾斯基“正方形”(概念上类似于谢尔宾斯基地毯,但经过简化)。

我的代码如下:

public static void drawQuadChaos(double ax, double ay, 
                                     double bx, double by,
                                     double cx, double cy,
                                     double dx, double dy,
                                     int nPoints) {
    Random rand = new Random();
    
    StdDraw.setPenRadius(0.03);
    StdDraw.setPenColor(StdDraw.RED);
    StdDraw.point(ax, ay);
    StdDraw.point(bx, by);
    StdDraw.point(cx, cy);
    StdDraw.point(dx, dy);
    
    StdDraw.setPenRadius(0.005);
    StdDraw.setPenColor(StdDraw.BLUE);
    
    double x = 0.0;
    double y = 0.0;
    
    for (int i = 0; i < nPoints; i++) {
        double choice = rand.nextInt(4);
        if (choice == 0){
            x = (x + ax) / 2.0;
            y = (y + ay) / 2.0;
        }
        else if (choice == 1){
            x = (x + bx) / 2.0;
            y = (y + by) / 2.0;
        }
        else if (choice == 2){
            x = (x + cx) / 2.0;
            y = (y + cy) / 2.0;
        }
        else if (choice == 3){
            x = (x + dx) / 2.0;
            y = (y + dy) / 2.0;
        }
        StdDraw.point(x, y);
    }
    
}

/*public static boolean storeDecisions(double x, double y) {
    while ( x != 0.0 && y != 0.0) {
        if (x % 3 == 1 && y % 3 == 1)
            return false;
        x /= 3;
        y /= 3;
    }
    return true;
}*/

public static void main(String[] args) {
    drawQuadChaos(0.1, 0.1, 0.1, 0.9, 0.9, 0.9, 0.9, 0.1, 20000);
}

我可以制作一个正方形,但与三角形不同的是,里面的点没有图案like so.

我认为问题在于重复点,我必须在循环之外创建一个变量,以记住所做的最后选择并在每次后续循环迭代中避免它。

我认为我需要在主循环之前创建一个变量,该变量的工作是记住 int,然后可以将该变量设置为每次循环迭代结束时所做的任何选择。然后,在下一次迭代时,它可以在做出新的随机选择时检查变量。

但是,我不确定如何编写该变量。虽然我可以使用数组或递归方法,但有人告诉我一个简单的 while 循环或 for 循环可以工作,但我不知道如何为这种特定情况安排它。我已经有这个问题好几天了,这是我最后的手段。有关如何解决此问题的任何建议?将不胜感激。

编辑:我想通了!我修改了for循环如下:

    double lastX = ax;
    double lastY = ay;
    
    double lastChoice = 0.0;
    
    for (int i = 0; i < nPoints; i++) {
        double choice = rand.nextInt(4);
        while (choice == lastChoice) {
            choice = rand.nextInt(4);
        }
        if (choice == 0){
            lastX = (lastX + ax) / 2.0;
            lastY = (lastY + ay) / 2.0;
        }
        else if (choice == 1){
            lastX = (lastX + bx) / 2.0;
            lastY = (lastY + by) / 2.0;
        }
        else if (choice == 2){
            lastX = (lastX + cx) / 2.0;
            lastY = (lastY + cy) / 2.0;
        }
        else if (choice == 3){
            lastX = (lastX + dx) / 2.0;
            lastY = (lastY + dy) / 2.0;
        }
        StdDraw.point(lastX, lastY);
        lastChoice = choice;
    }

这个修改在每次代码运行这个循环的时候重新定义了“choice”,并存储在“lastChoice”中,这样​​就不会重复之前的错误了。

【问题讨论】:

  • int x = 0; for (int i = 0; i &lt; 10; i++) { x = i; } System.out.println(x); 并不是说​​可以解决您的问题。这就是如何在循环之外声明变量。
  • 我很欣赏您的输入,但这似乎在结构上与我已有的 for 循环相似。两者有区别吗? (编辑:我认为挥之不去的大问题是我不知道如何将已建立循环 (x, y) 中生成的点放入另一个单独的 for 循环中。)
  • 或者只是在循环外定义选择,你不需要复制变量。

标签: java loops for-loop while-loop shapes


【解决方案1】:

一个很好的替代方法是在循环外实例化变量:

int i = 0;

然后使用一个while循环来达到同样的目的,并且每次在循环中增加变量。这样,一旦变量超出范围,您仍然会拥有该变量

int i = 0;
while (i<npoints){
  //... rest of your code
  i++;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 2014-11-11
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多