在 Y. Daniel Liang 的“Java 编程简介:综合版”的第四章末尾,我正在做一些编程练习。在问题 4.18 中,我提出了两种不同的解决方案。
哪种解决方案(PatternLoop1 或 PatternLoop2)更有效,为什么?
public class PatternLoop1 {
public static void main(String[] args) {
for (int counter = 0; counter < 6; ++counter) {
for (int counter2 = 0; counter2 <= counter; ++counter2) {
System.out.print(counter2 + 1);
}
System.out.println();
}
}
}
public class PatternLoop2 {
public static void main(String[] args) {
for (int counter = 1; counter < 7; counter++) {
for (int counter2 = 1; counter2 < 7 && counter2 <= counter; counter2++) {
System.out.print(counter2);
}
System.out.println();
}
}
}