switch case 编写菜单
时间: 2023-12-12 21:36:04 浏览: 180
以下是一个使用switch case编写菜单的例子:
```python
print("请选择您需要的服务:")
print("1. 查询余额")
print("2. 转账")
print("3. 存款")
print("4. 取款")
print("5. 退出")
choice = int(input("请输入您的选择:"))
# 使用switch case结构
def switch_case(choice):
switcher = {
1: "您选择了查询余额服务。",
2: "您选择了转账服务。",
3: "您选择了存款服务。",
4: "您选择了取款服务。",
5: "感谢您的使用,再见!",
}
return switcher.get(choice, "输入数据有误!")
print(switch_case(choice))
```
相关问题
java switch case
### 回答1:
Java中的switch case语句用于检查某个变量或表达式的值,然后根据值来执行相应的代码块。语法如下:
```
switch (expression) {
case value1:
// 当 expression 等于 value1 时执行的代码
break;
case value2:
// 当 expression 等于 value2 时执行的代码
break;
...
default:
// 当 expression 与所有 case 值都不匹配时执行的代码
}
```
expression可以是byte、short、char、int类型的变量,或者是返回这些类型值的表达式。case值必须是与expression相同类型的常量表达式。每个case块中的代码执行后必须使用break语句来终止switch语句,否则代码将继续执行到下一个case块。如果没有与expression匹配的case值,则执行default块中的代码。
### 回答2:
在Java中,switch case语句是一种用于多种条件下执行不同操作的控制结构。它可以用来替代多个if-else if语句,使代码看起来更简洁、易读。
switch(表达式) {
case 值1:
// 当表达式的值与值1匹配时执行的代码
break;
case 值2:
// 当表达式的值与值2匹配时执行的代码
break;
...
default:
// 当表达式的值与所有case都不匹配时执行的代码
}
switch后面的表达式可以是byte、short、int、char、enum、String类型。在每个case后面,我们可以编写与表达式值匹配时要执行的代码。在每个case的末尾,需要使用break语句来跳出switch语句,否则会继续执行后续的case代码。如果表达式的值与所有case都不匹配,那么会执行default块中的代码。
需要注意的是,在Java 7及以前,switch的表达式值只能是整数类型,不能是字符串,而在Java 7及以后,switch语句支持使用字符串作为表达式。
switch case语句的使用场景包括但不限于:
- 处理多个固定值的情况,例如菜单选择。
- 替代多个if-else if语句,使代码更易读。
- 状态机中的状态转换。
综上所述,switch case语句在Java中是一种非常有用的控制结构,适用于处理多个值的情况,提高了代码的可读性和简洁性。
android switch case 空指针
### 解决Switch Case中空指针异常的方法
在Android开发过程中,`switch case`语句如果处理不当确实可能引发空指针异常。为了有效预防此类问题的发生,在编写涉及`switch case`逻辑时应当采取一系列措施来增强代码健壮性和安全性。
#### 1. 验证对象非空
在执行任何可能导致访问未初始化变量的操作之前,先检查这些变量是否为空是非常重要的一步。这可以通过简单的条件判断实现:
```java
if (object != null){
switch(object.getValue()){
//case statements here...
}
}
else{
Log.e("Error", "Object is null");
}
```
此做法能够确保只有当目标对象存在时才会尝试读取其属性或调用方法[^1]。
#### 2. 使用默认分支捕获未知情况
即使已经验证过输入参数的有效性,仍然建议始终定义一个`default`子句作为最后的安全网。这样做不仅有助于捕捉意外状况下的潜在错误,还可以提高程序的整体稳定性。
```java
switch(someEnumValue){
case ENUM_VALUE_1:
doSomething();
break;
case ENUM_VALUE_2:
doAnotherThing();
break;
default:
handleUnexpectedCase(); // 或者抛出自定义异常
}
```
这种结构可以在枚举值发生变化或其他不可预见的情况下提供额外保护层[^2]。
#### 3. 应用MVP设计模式减少耦合度
采用Model-View-Presenter架构可以帮助分离业务逻辑与界面展示部分,从而降低因UI组件状态不确定而引起的空指针风险。具体来说就是让Presenter负责管理数据流并决定何时更新视图,而不是直接操作Activity内的控件实例。
例如,在加载菜单项前先确认对应资源已成功获取到再继续后续流程:
```java
public void loadMenuItems(){
if(menuAdapter!=null && menuListView!=null){
List<MenuItem> items = model.getMenuItems();
menuAdapter.setItems(items);
menuListView.setAdapter(menuAdapter);
} else {
view.showErrorMessage(R.string.error_loading_menu);
}
}
```
这种方法遵循了依赖注入原则,使得各模块之间的交互更加清晰可控[^4]。
#### 4. 利用手动缓存机制保存临时结果
对于那些频繁创建销毁的对象(如对话框),可以考虑引入局部静态成员变量来进行生命周期管理。一旦某个特定条件下该对象被实例化,则将其存储起来供下次快速重用;反之则清空引用以便及时释放内存空间。
```java
private static AlertDialog alertDialog;
//...
if(alertDialog==null || !alertDialog.isShowing()){
alertDialog=newBuilder().create();
alertDialog.show();
}else{
alertDialog.dismiss();
alertDialog=null; // 清除旧的弹窗引用
}
```
上述策略同样适用于其他类型的动态组件,比如自定义Toast提示框等[^5]。
阅读全文
相关推荐
















