Java中使用递归的文本金字塔
我正在尝试用任何文本输入制作一个完整的三角形。例如,如果我的字符串是“abcdefghij”,我希望结果是Java中使用递归的文本金字塔,java,recursion,Java,Recursion,我正在尝试用任何文本输入制作一个完整的三角形。例如,如果我的字符串是“abcdefghij”,我希望结果是 aj abij abchij abcdghij abcdefghij 如果字符串长度为奇数,如“abcdefghij”中所示,则输出为 a abi abchi abcdghi abcdefghi 这是我到目前为止所做的,但是我对单词的输出是颠倒的。我的输出是 abcdefghij abcdghij abchij abij aj
aj
abij
abchij
abcdghij
abcdefghij
如果字符串长度为奇数,如“abcdefghij”中所示,则输出为
a
abi
abchi
abcdghi
abcdefghi
这是我到目前为止所做的,但是我对单词的输出是颠倒的。我的输出是
abcdefghij
abcdghij
abchij
abij
aj
我到目前为止所做的事情
public static void main(String[] args) {
solve("abcdefghij");
}
public static void solve(String word) {
solve(word, word.length()/2-1);
}
public static void solve(String word, int it) {
// print starting spaces
for(int i = 0; i < it; i++)
System.out.print(" ");
// print out string
System.out.print(word+"\n");
if(word.length() > 2) {
int newlengthperside = (word.length() - 2)/2;
solve( word.substring(0, newlengthperside) + word.substring(word.length() - newlengthperside), it-1);
}
}
publicstaticvoidmain(字符串[]args){
解决(“abcdefghij”);
}
公共静态void solve(字符串字){
求解(word,word.length()/2-1);
}
公共静态void solve(字符串字,int-it){
//打印起始空格
for(int i=0;i2){
int newlengthperside=(word.length()-2)/2;
求解(word.substring(0,newlengthperside)+word.substring(word.length()-newlengthperside),it-1);
}
}
我只需要一个关于如何从aj开始而不是结束的建议。谢谢你的帮助这是家庭作业,因此我们非常感谢您的提示。您的代码应该如下所示:
public void solve(String str) {
for(int i=1;i<=str.length()/2;i++) {
for(int j=str.length()/2-i; j>0 ;j--) {
System.out.print(" ");
}
System.out.print(str.substring(0,i));
System.out.print(str.substring(str.length()-i));
System.out.println();
}
}
输出:
aj
abij
abchij
abcdghij
abcdefghij
这只涵盖了快乐之路,但你明白其中的逻辑
编辑: 对于递归方法:
public static void solve(String word) {
solve(word, 0);
}
public static void solve(String word, int it) {
// print starting spaces
String spaces="";
for(int i = 0; i < it; i++)
spaces+=" ";
if(word.length() > 2) {
int newlengthperside = (word.length() - 2)/2;
solve(word.substring(0, newlengthperside) + word.substring(word.length() - newlengthperside), it + 1);
}
System.out.print(spaces+word+"\n");
}
输出:
aj
abij
abchij
abcdghij
abcdefghij
将“//打印输出字符串”行与递归调用交换:
public static void solve(String word, int it) {
if(word.length() > 2) {
int newlengthperside = (word.length() - 2)/2;
solve( word.substring(0, newlengthperside) + word.substring(word.length() - newlengthperside), it-1);
}
// print out string
System.out.print(word+"\n");
}
这将首先输出最短的字符串,然后当函数返回时,它将输出下一个最大的字,以此类推。你必须自己算出空格部分,但这应该给你一个开始(我认为位于递归和“//print out string”区域之间的现有循环将起作用)。在反复调用恢复顺序后开始打印。@zubergu-遗憾的是,这不是一个答案,是对家庭作业相关问题的完美回答。无法投票支持现有答案作为家庭作业问题的代码解决方案,因此我将+1你的评论。我相信他需要使用递归来解决这个家庭作业问题(如标题所示)。我的坏朋友错过了这一点!“这至少应该给他一个逻辑的概念。”Stackflow说,谢谢你的帮助。我相信我可以尝试使用该算法并使用递归应用它。
solve("abcdefghij");
aj
abij
abchij
abcdghij
abcdefghij
public static void solve(String word, int it) {
if(word.length() > 2) {
int newlengthperside = (word.length() - 2)/2;
solve( word.substring(0, newlengthperside) + word.substring(word.length() - newlengthperside), it-1);
}
// print out string
System.out.print(word+"\n");
}