gpt4 book ai didi

java - 使用嵌套 switch 语句太多有问题吗?

转载 作者:行者123 更新时间:2023-12-02 01:56:39 26 4
gpt4 key购买 nike

我从未使用过嵌套 switch 语句。只是想知道在我的代码中使用嵌套 switch 语句是否有效。我有 3 个整数变量。 typeposition01 以及带有 item 06。我的代码太长了,无论如何要缩短它:

public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
type = spinner_type.getSelectedItemPosition();
position = spinner_pos.getSelectedItemPosition();
item = spinner.getSelectedItemPosition();
switch (adapterView.getId()) {
case R.id.cck_spinner_type:
switch (pos) {
case 0:
if(position == 0 && item == 0) {
sp_name = "cc_left";
} else if(position == 0 && item == 1){
sp_name = "cc_left1";
} else if(position == 0 && item == 2){
sp_name = "cc_left2";
}else if(position == 0 && item == 3){
sp_name = "cc_left3";
}else if(position == 0 && item == 4){
sp_name = "cc_left4";
}else if(position == 0 && item == 5){
sp_name = "cc_left5";
}else if(position == 0 && item == 6){
sp_name = "cc_left6";
}else if(position == 1 && item == 0) {
sp_name = "cc_right";
} else if(position == 1 && item == 1){
sp_name = "cc_right1";
} else if(position == 1 && item == 2){
sp_name = "cc_right2";
}else if(position == 1 && item == 3){
sp_name = "cc_right3";
}else if(position == 1 && item == 4){
sp_name = "cc_right4";
}else if(position == 1 && item == 5){
sp_name = "cc_right5";
}else if(position == 1 && item == 6){
sp_name = "cc_right6";
}
break;
case 1:
if(position == 0 && item == 0) {
sp_name = "ccL_left";
} else if(position == 0 && item == 1){
sp_name = "ccL_left1";
} else if(position == 0 && item == 2){
sp_name = "ccL_left2";
}else if(position == 0 && item == 3){
sp_name = "ccL_left3";
}else if(position == 0 && item == 4){
sp_name = "ccL_left4";
}else if(position == 0 && item == 5){
sp_name = "ccL_left5";
}else if(position == 0 && item == 6){
sp_name = "ccL_left6";
}else if(position == 1 && item == 0) {
sp_name = "ccL_right";
} else if(position == 1 && item == 1){
sp_name = "ccL_right1";
} else if(position == 1 && item == 2){
sp_name = "ccL_right2";
}else if(position == 1 && item == 3){
sp_name = "ccL_right3";
}else if(position == 1 && item == 4){
sp_name = "ccL_right4";
}else if(position == 1 && item == 5){
sp_name = "ccL_right5";
}else if(position == 1 && item == 6){
sp_name = "ccL_right6";
}
break;
}
break;

case R.id.cck_spinner_pos:
switch (pos) {
case 0:
if(type == 0 && item == 0) {
sp_name = "cc_left";
} else if(type == 0 && item == 1){
sp_name = "cc_left1";
} else if(type == 0 && item == 2){
sp_name = "cc_left2";
}else if(type == 0 && item == 3){
sp_name = "cc_left3";
}else if(type == 0 && item == 4){
sp_name = "cc_left4";
}else if(type == 0 && item == 5){
sp_name = "cc_left5";
}else if(type == 0 && item == 6){
sp_name = "cc_left6";
}else if(type == 1 && item == 0) {
sp_name = "ccL_left";
} else if(type == 1 && item == 1){
sp_name = "ccL_left1";
} else if(type == 1 && item == 2){
sp_name = "ccL_left2";
}else if(type == 1 && item == 3){
sp_name = "ccL_left3";
}else if(type == 1 && item == 4){
sp_name = "ccL_left4";
}else if(type == 1 && item == 5){
sp_name = "ccL_left5";
}else if(type == 1 && item == 6){
sp_name = "ccL_left6";
}
break;
case 1:
if(type == 0 && item == 0) {
sp_name = "cc_right";
} else if(type == 0 && item == 1){
sp_name = "cc_right1";
} else if(type == 0 && item == 2){
sp_name = "cc_right2";
}else if(type == 0 && item == 3){
sp_name = "cc_right3";
}else if(type == 0 && item == 4){
sp_name = "cc_right4";
}else if(type == 0 && item == 5){
sp_name = "cc_right5";
}else if(type == 0 && item == 6){
sp_name = "cc_right6";
}else if(type == 1 && item == 0) {
sp_name = "ccL_right";
} else if(type == 1 && item == 1){
sp_name = "ccL_right1";
} else if(type == 1 && item == 2){
sp_name = "ccL_right2";
}else if(type == 1 && item == 3){
sp_name = "ccL_right3";
}else if(type == 1 && item == 4){
sp_name = "ccL_right4";
}else if(type == 1 && item == 5){
sp_name = "ccL_right5";
}else if(type == 1 && item == 6){
sp_name = "ccL_right6";
}
break;
default:
Toast.makeText(getContext(),"ERR 208",Toast.LENGTH_SHORT).show();
}
break;
case R.id.cck_spinner:

switch (pos) {
case 0:
if (type == 0 && position == 0) {
sp_name = "cc_left";
} else if (type == 0 && position == 1) {
sp_name = "cc_right";
} else {
Toast.makeText(getContext(), "You can't change LABEL of this key", Toast.LENGTH_SHORT).show();
}
break;
case 1:
if (type == 0 && position == 0) {
sp_name = "cc_left1";
} else if (type == 0 && position == 1) {
sp_name = "cc_right1";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left1";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right1";
}
break;
case 2:
if (type == 0 && position == 0) {
sp_name = "cc_left2";
} else if (type == 0 && position == 1) {
sp_name = "cc_right2";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left2";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right2";
}
break;
case 3:
if (type == 0 && position == 0) {
sp_name = "cc_left3";
} else if (type == 0 && position == 1) {
sp_name = "cc_right3";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left3";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right3";
}
break;
case 4:
if (type == 0 && position == 0) {
sp_name = "cc_left4";
} else if (type == 0 && position == 1) {
sp_name = "cc_right4";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left4";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right4";
}
break;
case 5:
if (type == 0 && position == 0) {
sp_name = "cc_left5";
} else if (type == 0 && position == 1) {
sp_name = "cc_right5";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left5";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right5";
}
break;
case 6:
if (type == 0 && position == 0) {
sp_name = "cc_left6";
} else if (type == 0 && position == 1) {
sp_name = "cc_right6";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left6";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right6";
}
break;


default:
if (type == 0 && position == 0) {
sp_name = "cc_left";
} else if (type == 0 && position == 1) {
sp_name = "cc_right";
} else {
Toast.makeText(getContext(), "You can't change LABEL of this key", Toast.LENGTH_SHORT).show();
}
}
break;
}
pfKey4.setHint(sp.getString(sp_name, "not_found"));

}

最佳答案

所以看起来 sp_name 只是根据这 3 个变量和您没有涉及的 adapterView.getId() 进行更改。从你的代码来看,我相信如果 position 为 0,则名称已向左,如果为 1,则名称已向右。如果大于 0,则末尾的数字为 itemtype 0 表示以 cc_ 开头,type 1 表示 ccL_。我会创建一个简短的函数来创建 sp_name。

String sp_name="";
// type
if(type==0)
sp_name="cc_";
else
sp_name="ccL_";
//position
if(pos==0)
sp_name+="left";
else
sp_name+="right";
//item
if(item>0)
sp_name+=(item+"");
return sp_name;

关于java - 使用嵌套 switch 语句太多有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52190052/

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