我想通过递归函数分隔数字的每个数字。我正在尝试以下功能:
private static void print( int n ){
print( n/10, n%10 );
}
private static int print( int n, int ld ){
if( n < 10 ) return n;
int digit = n % 10;
int first = print( n/10, ld );
System.out.println( digit );
return first;
}
但是它不起作用。谁能帮助我如何更改上述功能以实现我的结果?
输入:12345 输出:1 2 3 4 5
我想通过递归函数来做到这一点
最佳答案
这将以正确的顺序打印数字。
private static void print( int n ){
int t = (int)Math.floor(n / 10);
if (t > 0) {
print(t);
System.out.println( n % (t * 10) );
}
else
System.out.println( n );
}
关于java - java中将每个数字与整数分开的递归数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59279631/