用while(true)和continue、break来实现在一个大循环里switch
2016-03-13 15:18
405 查看
while (true) { System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书"); String book; try { //取得整型命令 int command = inputCommand(); //根据不同命令值,进行不同操作 switch (command) { case 1://按照图书名称选择图书 book = getBookByName(books); System.out.println("book:" + book); break; case 2://按照图书序号(数组下标)选择图书 book = getBookByNumber(books); System.out.println("book:" + book); break; case -1://返回值为-1,说明输入有误 System.out.println("命令输入错误!请根据提示输入数字命令!"); continue; default://其他值的命令均认为是错误命令 System.out.println("命令输入错误!"); continue; } break;//退出程序 } catch (Exception bne) { //捕获”图书不存在异常“时,要求重新输入命令 System.out.println(bne.getMessage()); continue; } }
值得学习:
用while(true)和continue、break来实现在一个大循环里switch:
如果是选择了需要的“1”就break,如果是其他的(-1或者deafult)就用continue来反复循环,直到用户输入正确。
相关文章推荐
- STL list和deque
- NSThread、NSOperation/NSOperationQueue、GCD多线程
- Deep learning:四十四(Pylearn2中的Quick-start例子)
- poj-2478 Farey Sequence(dp,欧拉函数)
- iOS8新特性之基于地理位置的消息通知UILocalNotification
- Java中的continue、break和return
- Pop Sequence
- 子线程更新UI
- This Android SDK requires Android Developer Toolkit version 23.0.0 or above
- pintuer e.handler.apply is not a function
- 《iOS Human Interface Guidelines》——Map View
- How to Advertise Android as a Bluetooth LE Peripheral
- java中线程队列BlockingQueue的用法
- C#中扩展StringBuilder支持链式方法
- 02-线性结构3 Pop Sequence
- 使用LinkedList实现Stack与Queue
- UINavigationController, UITabBarController
- android 6.0 SystemUI源码分析(6)-StorageNotification介绍
- com.mchange.v2.resourcepool.CannotAcquireResourceException
- com.mchange.v2.resourcepool.CannotAcquireResourceException