【发布时间】:2021-09-03 21:10:24
【问题描述】:
任务是反向打印。每个元素的大小都比前一个大。所以当订单被打乱时它应该停止。
public class Program {
private void printWordRun(ArrayList<String> words) {
for(int i = words.size() - 1; i > 0; i--) {
String str = words.get(i);
if(str.length() < words.size()) {
System.out.println(str);
}
}
}
public static void main(String[] args) {
Program program = new Program();
program.testPrintWordRun();
}
private void testPrintWordRun() {
ArrayList<String> words = new ArrayList<>();
words.add("I");
words.add("am");
words.add("cat");
words.add("with");
words.add("happy");
words.add("dog");
words.add("sitting");
System.out.println("Testing printWordRun...");
printWordRun(words);
System.out.println();
}
}
打印应该是:
开心 和 猫 是 我
我明白了:
狗 快乐的 和 猫 是 我
【问题讨论】:
-
你的意思是每个元素都比前一个元素大一个?。据我了解,如果字符串的长度比下一个小一,您想打印。
-
我的任务是实现 printWordRun 以便打印从输入列表的开头开始运行的任何单词,单词。单词 run 应该以相反的顺序打印,每个单词单独一行。打印输出:“快乐”、“与”、“猫”、“我”、“我”。所以你可以看到每个单词的字母数比下一个大一个。我在代码中编写了 for 循环。我执行代码时的打印输出是:'dog'、'happy'、'with'、'cat'、'am'、'i'。
标签: java data-structures methods stack recursive-datastructures