12345
2345
345
45
5
在看到上面的代码时,可以确定2个循环将打印它
123456789
912345678
891234567
789123456
678912345
567891234
456789123
345678912
234567891
将打印多少个循环我们如何确定它
答案 0 :(得分:1)
您只需使用一个for循环打印两个模式:
String pattern = "12345";
int length = pattern.length();
for (int i = 0; i < length; i++) {
System.out.println(pattern);
//remove the first char after every iteration
pattern = pattern.substring(1);
}
System.out.println();
pattern = "123456789";
length = pattern.length();
for (int i = 0; i < length; i++) {
System.out.println(pattern);
String lastchar = pattern.substring(length-1);
String otherchars = pattern.substring(0, length-1);
//append last char to front after every iteration
pattern = lastchar.concat(otherchars);
}
输出:
12345
2345
345
45
5
123456789
912345678
891234567
789123456
678912345
567891234
456789123
345678912
234567891