Java switch double duplicate case(Java switch 双重重复的情况)
问题描述
我正在用 java 创建一个国际象棋游戏.
I'm creating a chess game with java.
您知道,当您开始国际象棋游戏时,每个队长"都有两个(抱歉,我不确定这个术语是什么)我创建了以下开关盒来创建图形的图形布局:
As you know when you start out the chess game you have two of each "Captains" (sorry I'm not sure what the term is) I have created the following switch case to create the graphical layout of the figures:
switch (j) {
case 1 || 8 : Rook tower = new Rook(""); return tower.getBrik();
case 2 || 7 :
case 3 || 6 : Bishop bishop = new Bishop(""); return bishop.getBrik();
case 4 : King king = new King(""); return king.getBrik();
case 5 : Queen queen = new Queen(""); return queen.getBrik();
}
getBrik() 方法是一个返回图像视图的节点.
Where the getBrik() method is a Node that returns an imageview.
现在您可以看到我的案例 2 和 3 是我尝试将两个案例合二为一的失败尝试.
Now as you can see my case 2 and 3 are my failed attempt to do two cases in one.
这甚至可能吗?如果可以,怎么办?
Is this even possible and if so how?
推荐答案
因为 fall through (执行继续到下一个 case
语句,除非你放一个 break;
在最后,或者当然,就像你的情况一样,一个 return
),你可以把这些情况放在一起:
Because of fall through (execution continues to the next case
statement unless you put a break;
at the end, or of course, as in your case, a return
), you can just put the cases under each other:
...
case 1:
case 8:
Rook tower = new Rook("");
return tower.getBrik();
case 3:
case 6:
Bishop bishop = new Bishop("");
return bishop.getBrik();
...
这篇关于Java switch 双重重复的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java switch 双重重复的情况


基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01