gpt4 book ai didi

java - java调用递归方法

转载 作者:行者123 更新时间:2023-12-01 04:57:52 26 4
gpt4 key购买 nike

我定义了一个递归方法(至少我相信它是递归的),它返回 void 并想在另一个方法中调用它,但不知道如何调用。我知道这是非常基本的,但是有人可以帮忙吗?谢谢。

递归方法:

private static void recursiveWhiteToBlack(BufferedImage image, int width, int height){
image.getRaster().setPixel(width,height, new int [] {0, 0, 0, 0, 0, 0});
int[][] neighbors = neighborsXY(width,height);

for(int i = 0; i<neighbors.length; i++){
int neighborX = neighbors[i][0];
int neighborY = neighbors[i][1];
int[] neighborColor = image.getRaster().getPixel(neighborX, neighborY, new int[] {0, 0, 0, 0, 0, 0});

if(neighborColor[0] == 1){
recursiveWhiteToBlack(image, neighborX, neighborY);
}
}
}

调用它:

public static BufferedImage countObjects(BufferedImage image, BufferedImage original, ComponentPanel panel){
BufferedImage target = copyImage(image);

for(int width=1; width<image.getRaster().getWidth()-1; width++){ //Determine the dimensions for the width (x)

for(int height=1; height<image.getRaster().getHeight()-1; height++){ //Determine the dimensions for the height (y)

int[] pixel = image.getRaster().getPixel(width, height, new int[] {0, 0, 0, 0, 0, 0});

if(pixel[0] == 1){
none = recursiveWhitetoBlack(image, width, height); //HOW TO CALL IT HERE!!!//

}

System.out.println("countObjects method called");
return target;

}

最佳答案

你可以这样调用它:

if(pixel[0] == 1){                      
recursiveWhitetoBlack(image, width, height);
}

由于该方法没有返回类型,因此不需要变量赋值。

关于java - java调用递归方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13806925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com