Java开关双重复情况 [英] Java switch double duplicate case

查看:47
本文介绍了Java开关双重复情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用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()方法是一个返回图像视图的Node.

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.

这甚至有可能吗?

推荐答案

由于失败(执行将继续到下一个 case 语句,除非您输入结束;或者,当然,就像您的情况一样,是 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开关双重复情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆