Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 Mandelbrot程序中遇到了一个无限循环_Java_Loops_Fractals - Fatal编程技术网

我想我在java Mandelbrot程序中遇到了一个无限循环

我想我在java Mandelbrot程序中遇到了一个无限循环,java,loops,fractals,Java,Loops,Fractals,我正在写一个程序,画一部分曼德布罗特分形。我很确定我在这条线上的某个地方碰到了一个无限循环。很可能是别的,但不管是什么事,我都想不出来。这是我的密码: Mandelbrot.java package edu.ycp.cs201.mandelbrot; import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.BufferedOutputStream;

我正在写一个程序,画一部分曼德布罗特分形。我很确定我在这条线上的某个地方碰到了一个无限循环。很可能是别的,但不管是什么事,我都想不出来。这是我的密码:

Mandelbrot.java

package edu.ycp.cs201.mandelbrot;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;

import javax.imageio.ImageIO;

public class Mandelbrot {
    private static final int HEIGHT = 600;

    private static final int WIDTH = 600;



    public static void main(String[] args) throws IOException {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter coordinates of region to render:");
        System.out.print("  x1: ");
        double x1 = keyboard.nextDouble();
        System.out.print("  y1: ");
        double y1 = keyboard.nextDouble();
        System.out.print("  x2: ");
        double x2 = keyboard.nextDouble();
        System.out.print("  y2: ");
        double y2 = keyboard.nextDouble();

        System.out.print("Output filename: ");
        String fileName = keyboard.next();

        keyboard.close();

        int[][] iterCounts = new int[HEIGHT][WIDTH];
        MandelbrotTask task = new MandelbrotTask(x1, y1, x2, y2, 0, WIDTH, 0, HEIGHT, iterCounts);  
        task.run();


        // TODO: create the rendering, save it to a file
        BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
        Graphics g = bufferedImage.getGraphics();
        System.out.print("things are happening");
        for (int i = 0; i < WIDTH; i++)
        {
            for (int j = 0; j < HEIGHT; j++)
            {

                if (iterCounts[i][j] < 10)
                {
                g.setColor(Color.BLACK);
                g.fillRect(i,j,1,1);
                }
                else 
                    g.setColor(Color.BLUE);
                    g.fillR

ect(i,j,1,1);
            }

    }
    g.dispose();



    OutputStream os = new BufferedOutputStream(new FileOutputStream(fileName));

    try {
        ImageIO.write(bufferedImage, "PNG", os);
    } finally {
        os.close();
    }
}
MandelbrotTask.java

package edu.ycp.cs201.mandelbrot;


public class MandelbrotTask implements Runnable {
    private double x1, y1, x2, y2;
    private int startCol, endCol, startRow, endRow;
    private int[][] iterCounts;


    public MandelbrotTask(double x1, double y1, double x2, double y2,
                          int startCol, int endCol, int startRow, int endRow,
                          int[][] iterCounts) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.startCol = startCol;
        this.endCol = endCol;
        this.startRow = startRow;
        this.endRow = endRow;
        this.iterCounts = iterCounts;

    }

    public void run() {
        System.out.print("Working...");
        for (int i = startRow; i < endRow; i++) {
            for (int j = startCol; j < endCol; j++) {
                  Complex c = getComplex(i, j);
                  int iterCount = computeIterCount(c);
                  iterCounts[i][j] = iterCount;
            }
        }
        System.out.print("donarino");

    }

    // TODO: implement getComplex and computeIterCount methods

    public Complex getComplex(double i, double j)
    {
        double k ,l;

         k = ((x2 - x1)/600)*i + x1;
         l = ((y2 - y1)/600)*j + y1;
        //System.out.println(" k :" + k + " l: " + l);

        return new Complex(k,l);
    }

    public int computeIterCount(Complex c)
    {
        Complex z = new Complex(0,0);
        int count = 0;
        while (z.getMagnitude() < 2 || count <= 11)
        {
            z = z.multiply(z).add(c);
            count++;
        }
        return count;
    }
}

您可以在每组for循环之前和之后进行线路调试。例如,您可以执行以下操作:

System.out.println("starting for loop on line: " + Thread.currentThread().getStackTrace()[1].getLineNumber());
for (int i = 0; i < WIDTH; i++)
{
    for (int j = 0; j < HEIGHT; j++)
    {
        // Your Code is here
    }
}
System.out.println("I am at line: " + Thread.currentThread().getStackTrace()[1].getLineNumber());

当其中一个输出没有显示时,您将知道什么没有完成。

我有几点建议

概述:

仅在函数中使用作为参数提供的数据,而不像从getComplex内部引用x1、x2。这两种解决方案都可以工作,但这使得函数可以独立调试,因为您向函数提供了所有数据。 曼德布罗特·怀斯:

首先创建一个可以正确计算单点像素的函数。单独测试。 然后将单像素函数组织成一个循环(最好是迭代),这样就不必管理循环。也测试一下这个。 在整个工作正常之前,不要为用户输入而烦恼。模拟输入数据。 如果代码可读且有效,则可以开始优化并请求用户输入。
单独运行每个循环集,查看是否完成。如果是这样的话,就不是那样了。试着把println语句放在可疑的循环中,比如ComputeInterCount中的一个循环。@mdnghtblue我已经试过了,但在某些地方,由于它运行循环的次数太多,它们就不会显示出来。@ryannady我不知道如何在Eclipse中做到这一点,但我会尝试把它放到add System.out.printlnHere;在每一组for之后,当它到达一个无止境的时候,你将看不到输出。好的,我这样做了,是MadelbrotTask内部运行中的循环没有完成,但我仍然不明白为什么。那么PDD打印驱动开发的下一个级别将是在循环中使用I&j打印。
System.out.println("starting for loop on line: " + Thread.currentThread().getStackTrace()[1].getLineNumber());
for (int i = 0; i < WIDTH; i++)
{
    for (int j = 0; j < HEIGHT; j++)
    {
        // Your Code is here
    }
}
System.out.println("I am at line: " + Thread.currentThread().getStackTrace()[1].getLineNumber());