gpt4 book ai didi

java - 将 switch 语句与 for 循环结合起来

转载 作者:行者123 更新时间:2023-11-30 04:03:44 24 4
gpt4 key购买 nike

我目前正在开发 Java GUI。现在我的问题是,如何以正确的方式将 switch 语句与 for 循环结合起来。目前的方式是可行的,但是需要花费大量时间来输入语句。

public void labelSwitchCase(int i, int userInput) {
// switch case on i
switch (i) {

case 0: gridLabels[i].setText("" + userInput);
break;

case 1: gridLabels[i].setText("" + userInput);
break;

case 2: gridLabels[i].setText("" + userInput);
break;

case 3: gridLabels[i].setText("" + userInput);
break;

case 4: gridLabels[i].setText("" + userInput);
break;

case 5: gridLabels[i].setText("" + userInput);
break;

case 6: gridLabels[i].setText("" + userInput);
break;

case 7: gridLabels[i].setText("" + userInput);
break;

case 8: gridLabels[i].setText("" + userInput);
break;
// no defoult
}
}

最佳答案

只需使用:

public void labelSwitchCase(int i, int userInput) {
gridLabels[i].setText("" + userInput);
}

关于java - 将 switch 语句与 for 循环结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21289139/

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