search
HomeComputer TutorialsComputer KnowledgeNot familiar with Java recursive method code

Not familiar with Java recursive method code

java code does not understand recursive methods

This is also a loop method. It may be difficult for beginners to understand... Let me explain

For example, the parameter in the fun() method is 100. Let me change it to 2.

The purpose of this recursive method is accumulation. The result is the same as the loop accumulation, but the execution method is different.

The process of program execution is as follows:

When you pass 2 in, the program will execute the content in else

That is return temp fun(temp-1);

The actual return is: 2 fun(2-1);

That is: 2 fun(1) changes the original parameter 3 to 2;

Let me start. This is an accumulation program, so take out 3 and assign it to the sum defined before, so the current sum=2;

Because this is a recursive method, the fun(int temp) method needs to be executed repeatedly;

It’s just that the parameter now becomes 1

So the next step will be like this:

Because the parameter is 1, it will enter the if

So it will return 1;

So the current sum should be the previous sum plus the 1 returned by the current fun(1)

so.....now sum should be: 2 1=3;

I just cited the cycle between two numbers. The cycle between the other 98 numbers is also like this

In short, recursion means to keep calling yourself until the conditions cannot be met, then it will not call itself.

Take a look, it may be a bit much. I want to explain it more clearly. I have encountered it before, but I have never asked anyone else. If you don’t understand or don’t understand, ask me again...

Java Recursive Algorithm If you have high income, please come in! Use recursion to implement

The work is very beautiful. If you can make an interface of this level, this small problem should not be difficult for you.

The question here is:

1. To design the structure of this graph, the easiest way is to use an array.

2. How to enumerate connected nodes. Simply put, it is a question of judging upper left, upper right, left, right, lower left, and lower right.

3. How to determine the same color in sequence? The simplest algorithm is the flooding method. Just start looking from the 6 directions above. After finding the next point, start looking from the 6 directions. . .

The approximate code is as follows:

class Rabbit

{

final int D_UP_LEFT = 1 ;

final int D_UP_RIGHT = 2;

final int D_LEFT = 3;

final int D_RIGHT = 4;

final int D_DOWN_LEFT = 5;

final int D_DOWN_RIGHT = 6;

getColor () ;

getCloseRibbit (int direction)

{

... // Here is the function to obtain adjacent rabbits in the specified direction

// If not return null .

}

int getColor () {}

List checkColor ()

{

ArrayList list = new ArrayList () ;

doCheckColor (list, this) ;

return list ;

}

void doCheckColor (List list, Rabbit r)

{

if (r.getColor () != this.getColor () || list.contains (r))

return ;

list.add (this) ;

for (int i = D_UP_LEFT ; i

{

Rabbit next = r.getCloseRibbit (i) ;

if (next != null)

next.doCheckColor (list, r) ;

}

}

}

The resulting list is the bunny of the same color you want.

How does JAVA use the recursive method to get nn from 1 to m

Recursion using arrays:

public class Test12 {

static int M = 4;

static int N = 3;

static int[] a= new int[]{1,2,3,4};

static int[] b = new int[N];

public static void main(String[] args){

C(M,N);

}

static void C(int m,int n){

int i,j;

for(i=n;i

b[n-1] = i-1;

if(n>1)

C(i-1,n-1);

else {

for(j=0;j

System.out.print(a[b[j]] " ");

System.out.println();

}

}

}

}

Output:

1 2 3

1 2 4

1 3 4

2 3 4

java implements recursive operation n! If n is not an integer, you will be prompted to re-enter

java implements recursive operation n! , enter n, if it is not an integer, you will be prompted to re-enter...

port java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Test {

public int jiecheng(int num) {// Recursive factorial

if (num > 1)

return num * jiecheng(num - 1);

else if (num == 1)

return 1;

else

return 0;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in); // Get input from the keyboard

String num = """;

Pattern p = Pattern.compile("\\d "); // Regular expression, matching (1 to N digits) integer

Matcher m = null;

int k = 0;

while (true) {

System.out.print ("Please enter an integer:");

num = sc.nextLine(); // Get a line of input

m = p.matcher(num);

if (m.matches()) {

k = Integer.valueOf(num); // Convert string to integer

break;

} else

System.out.println ("Not an integer, please re-enter!");

System.out.println();

}

System.out.println(new Test().jiecheng(k)); //Call factorial method

}

}

Happy to live in a treasured land and prosper for thousands of years. May everything be prosperous for your family and your family. Happy to welcome the new year

The above is the detailed content of Not familiar with Java recursive method code. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Excel办公网. If there is any infringement, please contact admin@php.cn delete
4 Ways to Fix Taskbar Color Not Changing in Windows 10 - MiniTool4 Ways to Fix Taskbar Color Not Changing in Windows 10 - MiniToolMay 02, 2025 am 12:50 AM

Have you ever encountered the issue of “taskbar color not changing”? Do you feel confused about this problem? This post from php.cn focuses on this matter and provides you with several proven methods to help you fix it.

Remote Desktop Service Is Currently Busy - Targeted SolutionsRemote Desktop Service Is Currently Busy - Targeted SolutionsMay 02, 2025 am 12:49 AM

When trying to connect to a Windows Server, you may encounter the error that Remote Desktop Services is currently busy. Both the Windows Server settings and the user’s device might be the cause of this error. Whatever the causes, php.cn can help you

Fixed: Windows Shuts Down When Scanning for VirusesFixed: Windows Shuts Down When Scanning for VirusesMay 02, 2025 am 12:48 AM

Windows shuts down when scanning for viruses? Windows Defender full scan causes PC to shut down? What should you do when this issue occurs? In this post from php.cn, you will learn a couple of feasible solutions.

Network Error Message Appears in ChatGPT? Try 7 Ways to Fix! - MiniToolNetwork Error Message Appears in ChatGPT? Try 7 Ways to Fix! - MiniToolMay 02, 2025 am 12:47 AM

ChatGPT network error is a common issue that could happen when writing code or when the AI is writing long answers. What should you do if you encounter this issue? Don’t worry and go to try several ways collected by php.cn in this post to easily get

Fix Windows Update Check for Updates Grayed outFix Windows Update Check for Updates Grayed outMay 02, 2025 am 12:44 AM

One method to get Windows updates is to click the Check for updates button in Windows Update. But if the check for updates button is grayed out, do you know what you should do to fix the problem? In this post, php.cn Software will introduce some meth

Complete Guide to Fixing Outlook Not Saving Drafts on WinComplete Guide to Fixing Outlook Not Saving Drafts on WinMay 02, 2025 am 12:43 AM

Outlook draft emails are stored in the Drafts folder generally. However, sometimes you may encounter the issue of “Outlook not saving drafts”. In this situation, what should you do to find the draft emails and deal with this issue? In this post from

iCloud Mail Login/Sign-up | How to Access iCloud Mail PC/Android - MiniTooliCloud Mail Login/Sign-up | How to Access iCloud Mail PC/Android - MiniToolMay 02, 2025 am 12:42 AM

This post offers an iCloud Mail login and sign-up guide. You can create a free iCloud Mail account to use it to send or receive emails on the web or any device. Explanation for how to access iCloud Mail on Windows 10/11 or Android is also included.

Download and Install Windows ADK for Windows [Full Versions] - MiniToolDownload and Install Windows ADK for Windows [Full Versions] - MiniToolMay 02, 2025 am 12:41 AM

When you try to provision Windows 11/10, you need the corresponding version of Windows ADK (Windows Assessment and Deployment Kit) to help you finish the task. This post from php.cn teaches you how to download and install Windows ADK.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools