简体   繁体   English

递归方法如何开始打印整形

[英]Recursive method how it wok to print shap


I want to know how recursive method work to prints a large X composed of smaller X's with a given “width”, input number, which is guaranteed to be odd. 我想知道递归方法如何以给定的“宽度”(输入数字)打印由较小X组成的较大X,该数字一定是奇数。

the “width” is length (number) of X's along one line large X. “宽度”是X沿着大X线的长度(个数)。

Example for an X of width input number =3 the method will print this shape! X宽度输入数字= 3的示例,该方法将打印此形状!

 X X

  X

 X X

I try to solve this problem but I couldn't can anyone here help me .. in java code , 我尝试解决此问题,但是我无法在这里帮助任何人..在Java代码中,

This is my code he works good but prints wrong when numberinput=7 or 5 这是我的代码,他工作正常,但是当numberinput = 7或5时打印错误

 public static String shape(String i,int  numberinput) {   
 //error check, not working for even numbers
 if(numberinput%2 == 0)
    return null;

 //terminating condition, stop recursion when this occurs.
 if(numberinput == 1)
    return "X";

else

return "X"+" "+i+"\n" +" "+shape(" "+i,numberinput-2)+" "+"\n"+i+" "+"X";
}

he prints this when numberinput=5 他在numberinput = 5时打印

       X X

    X  X

    X 

    X X 

    X X

A valid recursive method should have two parts. 有效的递归方法应包含两个部分。

  1. Recursive call (call itself to do part of work) 递归调用(调用自身来完成部分工作)
  2. Terminating condition (A condition to stop the recursion) 终止条件(停止递归的条件)

You have a recursive call, but not a termination condition. 您有一个递归调用,但没有终止条件。 Hence your recursion won't stop until it fills up the entire stack and cause an exception. 因此,递归直到填满整个堆栈并引起异常后才会停止。 Hence you should include a terminating condition in your recursive method. 因此,您应该在递归方法中包括终止条件。

A sample implementation might look like this. 一个示例实现可能看起来像这样。

public static String shap(String i, int numberinput) {
    //error check, not working for even numbers
    if(numberinput%2 == 0)
        return null;

    //terminating condition, stop recursion when this occurs.
    if(numberinput == 1)
        return "X";

    //recursion, call recursive until terminating condition occurs.
    return "X" + i + shap(i, numberinput-2) + i + "X";
}

I have written a java code, in which recursion is only for the levels, to generate the string i have used a loop : 我已经编写了一个Java代码,其中递归仅适用于各个级别,以生成使用循环的字符串:

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        List<String> ans = new ArrayList<String>();
        shap(7, 1, ans);
        //System.out.println(ans);
        for(int i = 0;i < ans.size();i++){
            System.out.println(ans.get(i));
        }
    }

    public static void shap(int numberinput, int currentLevel, List<String> ans) {
        if(currentLevel == numberinput+1) return;
        String val = "";
        for(int i = 1;i <= numberinput;i++){
            if(i == currentLevel || i == (numberinput+1-currentLevel)) val += "X";
            else val += " ";
        }
        ans.add(val);
        shap(numberinput, currentLevel+1, ans);//Recursion step for the levels
    }
}

Link to solution on Ideone : http://ideone.com/RioL9g 链接到Ideone上的解决方案: http ://ideone.com/RioL9g

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM