# java main递归_Java递归简介

Java递归简介

Java中的递归定义为“方法直接或间接调用自身(相同方法)”。递归函数用于需要一次又一次地执行同一组操作直到达到结果的情况。它执行几次迭代，并且每次迭代问题说明都变得越来越简单。Java中的递归是一种基于对同一问题的较小块的解决方案来解决问题的方法。大部分无限可能性迭代都可以通过递归来解决。我们可以说递归是循环语句的另一种方法。如果我们没有正确使用递归函数，那么它将执行无限次。

returntype methodName() { //logic for application methodName();//recursive call }

1.直接递归调用

returntype methodName() { //logic for application methodName();//recursive call }

public class Factorial { static int fact(int i){ if (i == 1) return 1; else return(i * fact(i-1)); } public static void main(String[] args) { System.out.println("The factorial of given number 6 is: "+fact(6)); } }

2.间接/相互递归调用

firstIndirectRecursive() { // Logic secondIndirectRecursive(); } secondIndirectRecursive() { //Logic firstIndirectRecursive(); }

import java.util.Scanner; public class IndirectRecursion { public static boolean oddNum(int i) { if (i<0) throw new IllegalArgumentException("Number is negative"); if(i == 0){ return false; } else { return evenNum(i-1); } } public static boolean evenNum(int i) { if (i<0) throw new IllegalArgumentException("Number is negative"); if(i == 0){ return true; } else { return oddNum(i-1); } } public static void main(String[] args) { Scanner inputNum = new Scanner(System.in); System.out.print("Give a number: "); int n = inputNum.nextInt(); if (evenNum(n)) System.out.println(n + " is even"); else System.out.println(n + " is odd"); inputNum.close(); } }

Java中的递归示例

Example＃1 –斐波那契数列

public class FibonacciSeries{ static int num1=0,num2=1,num3=0; static void fibonacci(int n){ if(n>0){ num3 = num1 + num2; num1 = num2; num2 = num3; System.out.print(" "+num3); fibonacci(n-1); } } public static void main(String args[]){ int n=10; System.out.print(num1+" "+num2);//printing constant first two digits 0 and 1 fibonacci(n-2);//Since first two numbers are already done } }

public class TowerOfHanoi { public static void main(String[] args) { int count = 3; tower(count, 'A', 'B', 'C'); } public static void tower(int first, char disk1, char temp, char disk2) { if (first == 1) { System.out.println("Disk 1 from " + disk1 + " to " + disk2); } else { tower(first - 1, disk1, disk2, temp); System.out.println("Disk " + first + " from " + disk1 + " to " + disk2); tower(first - 1, temp, disk1, disk2); } } }

package com.recursion; import java.util.Scanner; public class FactorialOfNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Which number factorial do you want?=>"); //taking input from the user int input = scanner.nextInt(); System.out.println("Factorial of " + input + "! is=>"+getMyFactorialNumber(input)); scanner.close(); } public static long getMyFactorialNumber(int inputNumber) { if (inputNumber == 1)//base condition return 1; return inputNumber * getMyFactorialNumber(inputNumber - 1);//recursive call } }

import java.util.Scanner; //ARMSTRONG number means sum of numbers of cubes equal to the number public class ArmstrongNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter any Number?=>"); // taking input from the user int input = scanner.nextInt(); //calling isArmstrongNumber() method and put in a variable double checkNumber=isArmstrongNumber(input); //checking the number if(input==checkNumber) { System.out.println(input+" is ARMSTRONG NUMBER"); } else { System.out.println(input+" not is ARMSTRONG NUMBER"); } scanner.close(); } static int remainderNumber; static double total = 0; public static double isArmstrongNumber(int inputNumber) { if (inputNumber > 0) { remainderNumber = inputNumber % 10;//separating digits total = total + Math.pow(remainderNumber, 3);//cubes sum isArmstrongNumber(inputNumber / 10);//recursive call } return total; } }

Example＃5 –回文数

import java.util.Scanner; public class PalindromeNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter any Number=>"); // taking input from the user int input = scanner.nextInt(); int checkNumber = palindromeNumberOrNot(input,0); if (checkNumber == input) { System.out.println(input+" is a PALINDROME NUMBER"); } else { System.out.println(input+" is not a PALINDROME NUMBER"); } scanner.close(); } public static int palindromeNumberOrNot(int inputNumber,int baseNumber) { if (inputNumber == 0)// base case return baseNumber; baseNumber = (baseNumber * 10) + (inputNumber % 10);// getting the reverse of the number and stores in temp return palindromeNumberOrNot(inputNumber/10,baseNumber);//recursive call } }

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
11-03 4854
07-22 2707
02-24 301
04-29 1万+
08-26 5808
11-09 1241
09-11 2977

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。