我的程序没有错误,但它没有打印出我想要的

No errors in my program, but it is not printing out what i would like it to

我的作业如下:

Create a program which will remove the first and last characters of a string. The program should then remove the next set of outside characters. The program continues in this fashion until it has hit the middle character or the middle two characters, completing a Word Pyramid. using recursion

所以我编写了我的程序,它没有错误,但它没有打印出我创建的 "wordPyramid" 方法中的字符串。

如果用户输入单词"pyramid" 我希望程序输出:

金字塔
亚拉米
内存
一个

这是我的代码:

package wordpyramid;

import javax.swing.JOptionPane;

public class Wordpyramid {
    public static void main(String[] args) {
        //Ask user what word they would like to "pyramid"
        String input = JOptionPane.showInputDialog("What word would you like to " + "'pyramid'?");
        wordPyramid(input);
    }

    //Create word pyramid method
    public static String wordPyramid(String word) {
        int length = word.length();
        if (word.length() == 1) {
            return word;
        } else {
            return word = wordPyramid(word.substring(1, length-1));
        }
    }
}

您需要通过执行 System.out.println(word);

来打印字符串
public static String wordPyramid(String word) {
    int length = word.length();
    System.out.println(word);   //printing word
    if (word.length() == 1) {
        return word;
    } else {

        return word = wordPyramid(word.substring(1, length - 1));
    }

}

输出

pyramid
yrami
ram
a

Demo