How to use Recursion to find triangular numbers in java
This article shows you how to use recursion to find triangular numbers in java. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Package com.clean.array;import java.util.Scanner;/** * Recursive triangular digits * / public class TranTest {public static void main (String [] args) {TranTest tranTest = new TranTest (); Scanner scanner = new Scanner (System.in); while (true) {System.out.println ("please input an Integer value:"); String x = scanner.next () If (x.equalsIgnoreCase ("Q")) {System.out.println ("quit!"); break;} int y = Integer.parseInt (x); / / int v = tranTest.triange (y); int v = tranTest.triangeDg (y); System.out.println ("res =" + v) } private int triangeDg (int n) {System.out.println ("enter:" + n); if (n = 1) {System.out.println ("retrun 1"); return 1;} else {int temp = n + triangeDg (n-1); System.out.println ("return:" + temp); return temp * @ param n * @ return * / private int triange (int n) {int total = 0; while (n > 0) {total = total + n;-- n;} return total }} the above is how to use recursion to find triangular numbers in java. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.