递归调用后仍未发生递归。

huangapple go评论88阅读模式
英文:

Recursion does not take place even after recursive call

问题

尽管这个函数的性质是递归的,但它从不递归。我完全不明白为什么会发生这种情况。

public void fill(Graphics g, View v, Point interior, Color boundaryColor, Color interiorColor) {
    int x = interior.getx();
    int y = interior.gety();
    System.out.println("x: " + x + " y: " + y);
    int pixelColor = v.getPixel(x, y); //始终为负数或零
    
    if (pixelColor == 0) {
        System.out.println("退出情况");
        return;
    }
    
    if ((pixelColor != boundaryColor.getRGB()) && (pixelColor != interiorColor.getRGB())) {
        System.out.println("*");
        interior.drawPoint(g, v);
        
        int nextX, nextY;
        
        nextX = x + 1;
        nextY = y;
        
        Point p = new Point(v, nextX, nextY);
        
        this.fill(g, v, p, boundaryColor, interiorColor);
    }
}

说明:
View v、Graphics g 和 Point interior 是自定义对象;

如果函数是递归性质的,当达到退出条件时,可能会打印"退出情况";

代码执行结果:

递归调用后仍未发生递归。

英文:

Even though the function is recurssive in nature it never recurse. I am completely blank why this is happening.

public void fill(Graphics g, View v, Point interior, Color boundaryColor, Color interiorColor) {
int x = interior.getx();
int y = interior.gety();
System.out.println("x: "+x+" y: "+y);
int pixelColor = v.getPixel(x, y);//always negative or zero
	
if( pixelColor == 0) {
	System.out.println("Exit case");
	return;
}
	
if( (pixelColor != boundaryColor.getRGB()) && (pixelColor != interiorColor.getRGB())) {
	System.out.println("*");
	interior.drawPoint(g, v);
		
	int nextX, nextY;
		
	nextX = x+1;
	nextY = y;
				
	Point p= new Point(v, nextX, nextY);
		
	this.fill(g, v, p, boundaryColor, interiorColor);
}
	
}

Explanation:
View v, Graphics g and Point interior are custom objects;

If the function has been recursive in nature it might have printed "Exit Case when the exit case is reached"

Code execution results:

递归调用后仍未发生递归。

答案1

得分: 1

你的函数递归运行得很好,就如其输出的结果所示。然而,它没有打印出"退出情况",这是因为pixelColor从未为0。调试你的代码,打印出pixelColor的值,尝试找出它为什么始终不为0。

英文:

Your function is recursing just fine as shown by the output it produces. What it isn't doing is printing "Exit case" because pixelColor is never 0. Debug your code/print the values of pixelColor and try to find out why it's never 0.

答案2

得分: 1

变量_pixelColor_ 似乎没有达到 0,这就是为什么您的代码没有打印出 "Exit Case"。

英文:

Variable pixelColor does not seems to be reaching 0 that is why your code is not printing "Exit Case"

huangapple
  • 本文由 发表于 2020年9月23日 20:47:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64028338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定