当前位置: 首页 > news >正文

java语法(switch)

新 单值
// switch
public class Main {
public static void main(String[] args) {
String fruit = "apple";
int opt = switch (fruit) {
case "apple" -> 1;
case "pear", "mango" -> 2;
default -> 0;
}; // 注意赋值语句要以;结束
System.out.println("opt = " + opt);
}
}

新复杂
public class Main {
public static void main(String[] args) {
String fruit = "orange";
int opt = switch (fruit) {
case "apple" -> 1;
case "pear", "mango" -> 2;
default -> {
int code = fruit.hashCode();
yield code; // switch语句返回值
}
};
System.out.println("opt = " + opt);
}
}


// switch
public class Main {
public static void main(String[] args) {
String fruit = "apple";
switch (fruit) {
case "apple":
System.out.println("Selected apple");
break;
case "pear":
System.out.println("Selected pear");
break;
case "mango":
System.out.println("Selected mango");
break;
default:
System.out.println("No fruit selected");
break;
}
}
}

http://www.jsqmd.com/news/13042/

相关文章:

  • 070_尚硅谷_其它进制转十进制
  • 详细介绍:微服务与面向服务编程(SOA)入门指南:从架构演进到 Spring Cloud 实践(初学者友好版)
  • python中修改局部json的思路
  • LSNet
  • 部署 GitLab 服务器 - 实践
  • Webpack 构建速度优化
  • [模拟赛] 过关(pass)
  • 2025.10.13
  • 第十三节:基于 Redis+MQ+DB实现高并发秒杀下的扣减
  • c++初体验
  • 四则运算错题本和错题重做的建立
  • 行列式的性质
  • 04_SQL语句一
  • 死锁的原因、表现以排查
  • 详细介绍:【C++】二叉搜索树
  • 朱世乐的 Johnson 算法笔记
  • 完整教程:文心大模型4.5:百度推出的新一代原生多模态基础大模型
  • day010
  • 详细介绍:DVWA | XSS 跨站脚本注入
  • 实用指南:JVM栈溢出时如何dump栈信息?
  • 1分钟Get宠物神兽壁纸我家猫被问疯了!
  • Zabbix 6.0+ 运用官方模板监控 Redis 数据库的完整安装指南
  • 【图论】Floyd算法简析
  • perl-Test-Simple-1.302195-5.fc39.noarch.rpm 怎么安装?Fedora 39 安装步骤讲解
  • Python的typing模块:类型提示 (Type Hinting) - 详解
  • MyEclipse 2017 激活教程
  • 插入 dp
  • 麒麟系统中修改 WPS 默认新建文件格式的方法 - 实践
  • 05_Mysql与图片的存储
  • 【Linux】权限 - 实践