java white case语句_java练习题,关于switch语句问题.

importjava.util.*;classGenerator{intkey=0;publicStringnext(){switch(key){default:case0:key++;return"SnowWhite";case1:key++;return"Bashful";case2:key++;return"Doc";case3:k...

import java.util.*;

class Generator {

int key = 0;

public String next() {

switch(key) {

default:

case 0 : key++; return "Snow White";

case 1 : key++; return "Bashful";

case 2 : key++; return "Doc";

case 3 : key++; return "Dopey";

case 4 : key++; return "Grumpy";

case 5 : key++; return "Happy";

case 6 : key++; return "Sleepy";

case 7 : key = 0; return "Sneezy";

}

}

public void fillA(String[] a) {

for(int i = 0; i < a.length; i++)

a[i] = next();

}

public Collection fill(Collection c, int n) {

for(int i = 0; i < n; i++) c.add(next());

return c;

}

}

public class Ex4 {

public static void main(String[] args) {

Generator gen = new Generator();

String[] a = new String[10];

gen.fillA(a);

for(String s : a) System.out.print(s + ", ");

System.out.println();

System.out.println(gen.fill(new ArrayList(), 10));

System.out.println(gen.fill(new LinkedList(), 10));

System.out.println(gen.fill(new HashSet(), 10));

System.out.println(gen.fill(new LinkedHashSet(), 10));

System.out.println(gen.fill(new TreeSet(), 10));

}

}

这个编译运行是没有问题的,下面这样改下:

import java.util.*;

class Generator {

int key = 0;

public String next() {

switch(key) {

case 0 : key++; return "Snow White";

case 1 : key++; return "Bashful";

case 2 : key++; return "Doc";

case 3 : key++; return "Dopey";

case 4 : key++; return "Grumpy";

case 5 : key++; return "Happy";

case 6 : key++; return "Sleepy";

case 7 : key = 0; return "Sneezy";

default:

}

}

public void fillA(String[] a) {

for(int i = 0; i < a.length; i++)

a[i] = next();

}

public Collection fill(Collection c, int n) {

for(int i = 0; i < n; i++) c.add(next());

return c;

}

}

public class Ex4 {

public static void main(String[] args) {

Generator gen = new Generator();

String[] a = new String[10];

gen.fillA(a);

for(String s : a) System.out.print(s + ", ");

System.out.println();

System.out.println(gen.fill(new ArrayList(), 10));

System.out.println(gen.fill(new LinkedList(), 10));

System.out.println(gen.fill(new HashSet(), 10));

System.out.println(gen.fill(new LinkedHashSet(), 10));

System.out.println(gen.fill(new TreeSet(), 10));

}

}

把switch语句中的"default:"放到了后面,编译错误:缺少返回语句。

我真的看不出来这两种方式有什么差别,求高人指点下,最好说详细些。

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
static void Custom_Setting_To_InitStc(uint8_t u8Key_Function) { switch(u8Key_Function) { case WHITEBALANCE: Key_WhiteBalance(); break; case IMAGEFREEZE: Key_ImageFreeze(); break; case ZOOM_IN: Key_ZoomIn(); break; case ZOOM_OUT: Key_ZoomOut(); break; case PHOTO: Key_Photo(); break; case VIDEO: Key_Video(); break; default: break; } } void Dealwith_Key(void) { AD_KeyPolling(); //check AD conversion result switch(GetKey()) // Get the logical key, and implement the functions { case KEY_ZOOM_OUT: Custom_Setting_To_InitStc(g_stcSetting.stcPanelSetting.stcCustomSetting.u8Top_ShortRelease); break; case KEY_MENU: //top key middle press Custom_Setting_To_InitStc(g_stcSetting.stcPanelSetting.stcCustomSetting.u8Top_MiddlePress); break; case KEY_LEFT_SHORT: //left key release Custom_Setting_To_InitStc(g_stcSetting.stcPanelSetting.stcCustomSetting.u8Left_ShortRelease); break; case KEY_RECORD: //left key middle press Custom_Setting_To_InitStc(g_stcSetting.stcPanelSetting.stcCustomSetting.u8Left_MiddlePress); break; case KEY_ZOOM_IN: //down key release Custom_Setting_To_InitStc(g_stcSetting.stcPanelSetting.stcCustomSetting.u8Down_ShortRelease); break; case KEY_AWB: //down key middle press Custom_Setting_To_InitStc(g_stcSetting.stcPanelSetting.stcCustomSetting.u8Down_MiddlePress); break; case KEY_PHOTO: //right key release Custom_Setting_To_InitStc(g_stcSetting.stcPanelSetting.stcCustomSetting.u8Right_ShortRelease); break; case KEY_FREEZE: //right key middle press Custom_Setting_To_InitStc(g_stcSetting.stcPanelSetting.stcCustomSetting.u8Right_MiddlePress); break; default: break; } }优化这段代码
06-15
这段代码可以进行如下的优化: 1. 可以将各个按键的处理函数的名称修改为更加具有描述性的名称,例如 `Process_WhiteBalance()`、`Process_ImageFreeze()` 等。 2. 可以将 `Custom_Setting_To_InitStc()` 函数和 `Dealwith_Key()` 函数中的 `g_stcSetting.stcPanelSetting.stcCustomSetting` 的部分提取出来,避免重复代码。例如: ``` const PanelCustomSetting_t& customSetting = g_stcSetting.stcPanelSetting.stcCustomSetting; switch(u8Key_Function) { case WHITEBALANCE: Process_WhiteBalance(customSetting.u8Top_ShortRelease); break; case IMAGEFREEZE: Process_ImageFreeze(customSetting.u8Top_MiddlePress); break; case ZOOM_IN: Process_ZoomIn(customSetting.u8Down_ShortRelease); break; case ZOOM_OUT: Process_ZoomOut(customSetting.u8Top_ShortRelease); break; case PHOTO: Process_Photo(customSetting.u8Right_ShortRelease); break; case VIDEO: Process_Video(); break; default: break; } ``` ``` AD_KeyPolling(); //check AD conversion result switch(GetKey()) // Get the logical key, and implement the functions { case KEY_ZOOM_OUT: Process_ZoomOut(customSetting.u8Top_ShortRelease); break; case KEY_MENU: //top key middle press Process_Menu(customSetting.u8Top_MiddlePress); break; case KEY_LEFT_SHORT: //left key release Process_LeftShort(customSetting.u8Left_ShortRelease); break; case KEY_RECORD: //left key middle press Process_Record(customSetting.u8Left_MiddlePress); break; case KEY_ZOOM_IN: //down key release Process_ZoomIn(customSetting.u8Down_ShortRelease); break; case KEY_AWB: //down key middle press Process_AWB(customSetting.u8Down_MiddlePress); break; case KEY_PHOTO: //right key release Process_Photo(customSetting.u8Right_ShortRelease); break; case KEY_FREEZE: //right key middle press Process_Freeze(customSetting.u8Right_MiddlePress); break; default: break; } ``` 3. 可以将不同的按键处理函数分别定义在不同的文件中,避免一个文件变得过于庞大,提高可读性和可维护性。 4. 可以将 `AD_KeyPolling()` 放在 `Dealwith_Key()` 函数的开头,而不是在 `switch` 语句中的每个 `case` 块中分别调用,这样可以避免重复代码。 ``` void Dealwith_Key(void) { AD_KeyPolling(); //check AD conversion result const PanelCustomSetting_t& customSetting = g_stcSetting.stcPanelSetting.stcCustomSetting; switch(GetKey()) // Get the logical key, and implement the functions { case KEY_ZOOM_OUT: Process_ZoomOut(customSetting.u8Top_ShortRelease); break; case KEY_MENU: //top key middle press Process_Menu(customSetting.u8Top_MiddlePress); break; case KEY_LEFT_SHORT: //left key release Process_LeftShort(customSetting.u8Left_ShortRelease); break; case KEY_RECORD: //left key middle press Process_Record(customSetting.u8Left_MiddlePress); break; case KEY_ZOOM_IN: //down key release Process_ZoomIn(customSetting.u8Down_ShortRelease); break; case KEY_AWB: //down key middle press Process_AWB(customSetting.u8Down_MiddlePress); break; case KEY_PHOTO: //right key release Process_Photo(customSetting.u8Right_ShortRelease); break; case KEY_FREEZE: //right key middle press Process_Freeze(customSetting.u8Right_MiddlePress); break; default: break; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值