Java 递归输出数字模式
Java recursion to output number pattern
所需输出:
5
454
34543
2345432
123454321
我如何使用递归来做到这一点?我的代码思路是:
public static void main(String[] args)
{
System.out.println(func(5));
}
public static String func(int num)
{
return num + "" +meth(num-1, num, num-1);
}
public static String meth(int start, int num, int end)
{
if(start==1)
{
return "1";
}
System.out.println(start+num+end);
return meth(start-1, num, end-1);
}
我对 if 语句和 System.out.println() 中 return 的内容感到困惑,
因为数字 5 不会 decrease/increase 因为它会保留,例如,它会在垂直方向保留 5,我该如何处理这个问题?
我的代码更像是一个例子,只是为了证明我在做。
也许这就是您要找的:
public class Main {
public static void main(String[] args) {
startRecursion(5);
}
private static void startRecursion(int number) {
String aligner = "";
for (int i = 0; i < number - 1; i++) {
aligner += " ";
}
recursion(String.valueOf(number), number, number, aligner);
}
private static void recursion(String value, int startNumber, int lastNumber, String aligner) {
if (lastNumber < 1) {
return;
}
if (lastNumber != startNumber) {
value = lastNumber + value + lastNumber;
}
System.out.println(aligner + value);
if (!aligner.isEmpty()) {
aligner = aligner.substring(0, aligner.length() - 1);
}
recursion(value, startNumber, lastNumber - 1, aligner);
}
}
打印:
5
454
34543
2345432
123454321
我认为只是通过参数传递 num 和前一个字符串(即前一行):
private static String meth(int num,String previous) {
String space="";
for(int i=0; i<num; i++) space+=" ";
//If number is negative, return empty String
if(num<=0) return "";
//if number is 1, we need to check if previous string is empty or not, because if is empty we need then umber only once, otherwise we need to add to the string
else if(num==1){
if(!previous.isEmpty()) return space+num+previous+num;
else return space+num+"";
}
//Here is checked if previous is empty and we do the same as before with number one
String currentRow=previous.isEmpty()? String.valueOf(num) : num+previous+num;
//We return the current row (with the current number), and we add the next row (or tree level) passing the number-1 and the row we have
return space+currentRow+"\n"+meth(num-1,currentRow);
}
所需输出:
5
454
34543
2345432
123454321
我如何使用递归来做到这一点?我的代码思路是:
public static void main(String[] args)
{
System.out.println(func(5));
}
public static String func(int num)
{
return num + "" +meth(num-1, num, num-1);
}
public static String meth(int start, int num, int end)
{
if(start==1)
{
return "1";
}
System.out.println(start+num+end);
return meth(start-1, num, end-1);
}
我对 if 语句和 System.out.println() 中 return 的内容感到困惑, 因为数字 5 不会 decrease/increase 因为它会保留,例如,它会在垂直方向保留 5,我该如何处理这个问题? 我的代码更像是一个例子,只是为了证明我在做。
也许这就是您要找的:
public class Main {
public static void main(String[] args) {
startRecursion(5);
}
private static void startRecursion(int number) {
String aligner = "";
for (int i = 0; i < number - 1; i++) {
aligner += " ";
}
recursion(String.valueOf(number), number, number, aligner);
}
private static void recursion(String value, int startNumber, int lastNumber, String aligner) {
if (lastNumber < 1) {
return;
}
if (lastNumber != startNumber) {
value = lastNumber + value + lastNumber;
}
System.out.println(aligner + value);
if (!aligner.isEmpty()) {
aligner = aligner.substring(0, aligner.length() - 1);
}
recursion(value, startNumber, lastNumber - 1, aligner);
}
}
打印:
5
454
34543
2345432
123454321
我认为只是通过参数传递 num 和前一个字符串(即前一行):
private static String meth(int num,String previous) {
String space="";
for(int i=0; i<num; i++) space+=" ";
//If number is negative, return empty String
if(num<=0) return "";
//if number is 1, we need to check if previous string is empty or not, because if is empty we need then umber only once, otherwise we need to add to the string
else if(num==1){
if(!previous.isEmpty()) return space+num+previous+num;
else return space+num+"";
}
//Here is checked if previous is empty and we do the same as before with number one
String currentRow=previous.isEmpty()? String.valueOf(num) : num+previous+num;
//We return the current row (with the current number), and we add the next row (or tree level) passing the number-1 and the row we have
return space+currentRow+"\n"+meth(num-1,currentRow);
}