gpt4 book ai didi

java - 哪个代码更有效?为什么?

转载 作者:行者123 更新时间:2023-11-29 09:41:40 26 4
gpt4 key购买 nike

<分区>

我正在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();
}
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com