gpt4 book ai didi

java - if-else 和 if return 有什么区别,哪个更好?

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

<分区>

这是我通常的编码方式:

public void foo (int x) {
if (x == 1) {
System.out.println("You entered one!");
} else if (x == 2) {
System.out.println("You entered two!");
} else if (x == 3){
................. and so on.

我意识到以下也会产生相同的结果:

public void foo (int x) {
if (x == 1) {
System.out.println("You entered one!");
return;
}
if (x == 2) {
System.out.println("You entered two!");
return;
}
if (x == 3) {
...... and so on.

即使它们产生相同的结果,使用哪个更好?为什么?我意识到有一个 switch 语句。

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