java - 将 if 语句转换为 swicth
问题描述
可以把这个程序变成一个 switch 语句吗?
if (Month * Day == Year){
System.out.println("The date is magic");
} else {
System.out.println("The date is not magic");
}
解决方案
要发表switch
声明,您需要constant
表达如下:
switch (month * day) {
case 2000:
System.out.println("The date is magic");
break;
default:
System.out.println("The date is not magic");
}
但是你不能使用 a variable
,你会constant expression required
在编译时得到
switch (month * day) {
case year:
还
switch
用于多重比较,而不仅仅是一个- 请遵循 Java 命名约定 :
attributes
,variables
,parameters
,method
必须以小写开头
推荐阅读
- php - 从现有文件夹恢复静态应用程序
- excel - 如何找到不同年份和日期的宿舍
- java - javac 11 可以编译将在 java 8 JVM 上运行的二进制文件/jar 吗?
- c++ - live555 会在 NAL 单元发出之前解析它们吗
- java - glDrawElements 上的 GL_INVALID_ENUM
- javascript - 在运行时查找当前处于滚动状态的 div。-苹果浏览器
- sql - 尺寸模型
- google-sheets - 通过 Zapier 将 Airtable 中的多条记录拉入 Google 表格
- android - 在kotlin中将背景颜色设置为MaterialCardView?
- json - 为什么除了纬度和经度之外的所有内容的 JSON 都返回为零?