英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论