简体   繁体   English

Java Switch语句混乱

[英]Java Switch statement confusion

In my app (which is an Android game), I have a method which checks if the player is still alive, and if not, runs an animation and the game is over. 在我的应用程序(这是一个Android游戏)中,我有一种方法可以检查玩家是否还活着,如果不是,则运行动画并结束游戏。

There are currently, 3 different animations available depending on how the player loses. 根据玩家的输家方式,目前有3种不同的动画可用。

So, for example, they can fall off the screen, hit a bird, or get squash by something falling from the sky. 因此,例如,它们可能从屏幕上掉下来,撞到鸟或被从天上掉下来的东西压扁。 Each one has a different animation. 每个人都有不同的动画。

Basically, what I'm doing is this: 基本上,我正在做的是这样的:

When the player loses, I set the method by which it happened, so for example, if they were hit by a bird: 当玩家失败时,我设置发生错误的方法,例如,如果它们被鸟击中:

hero.setKilledBy(hero.HITBIRD);

Then I act of this by switching within my checkGameOver() method. 然后,通过在我的checkGameOver()方法内进行切换来执行此操作。 However, I'm confused. 但是,我很困惑。 I have to check it like so: (Note, in my cases, I have to put 0, 1, 2): 我必须像这样检查它:(请注意,在我的情况下,我必须输入0、1、2):

switch(hero.killedBy()){

    case 0: {                            
        fallOffScreenAnimation();break;
    }

    case 1: {
        hitBirdAnimation();break;
    }
    case 2: {
        squashedAnimation();
    }
}

within my hero object's class, I have the following methods 在我的英雄对象的类中,我有以下方法

int killedBy;
final int FELLOFFSCREEN = 0;
final int HITBIRD = 1;
final int SQUASHED = 2;

int killedBy(){

    return killedBy;

}

int setKilledBy(int value){

    killedBy = value;

}

So, my question is, why can't I do something like this: 所以,我的问题是,为什么我不能做这样的事情:

switch(hero.killedBy()){

    case hero.HITBIRD {
        fallOffScreenAnimation();break;
    }

 //............... and so on.......

Within the switch statement, my hero object, isn't recognised? 在switch语句中,无法识别我的英雄对象吗? Why is this? 为什么是这样? If I just type it anyway, (case hero.HITBIRD), I get this error: 如果无论如何我都键入它(case hero.HITBIRD),则会出现此错误:

case expressions must be constant expressions case表达式必须是常量表达式

Obviously, for readability, I would much prefer to use the variable names I've set rather than the raw numerical values...... 显然,出于可读性考虑,我宁愿使用我设置的变量名,而不是原始数值。

Make your int constants static : 使您的int常量为static

final static int FELLOFFSCREEN = 0;
final static int HITBIRD = 1;
final static int SQUASHED = 2;

As class constants, the values available and known before an instance of Hero is created. 作为类常量,在创建Hero实例之前可用和已知的值。


However, for these reasons I would make these an enum and either switch on that, or have methods on the enum of what action to take when the hero dies because of it thus avoiding the switch altogether. 但是,由于这些原因,我将使它们成为一个枚举,或者打开该枚举,或者对枚举英雄死于该枚举时采取的措施具有枚举的方法,从而完全避免切换。

You can only use constants in cases. 您只能在情况下使用常量。 Your variables are instance variables, not constants. 您的变量是实例变量,而不是常量。 Each hero has a copy of those variables. 每个英雄都有这些变量的副本。 They should be declared as: 它们应声明为:

static final int FELLOFFSCREEN = 0;
static final int HITBIRD = 1;
static final int SQUASHED = 2;

And your should access them using the class name, not the object: 您应该使用类名而不是对象来访问它们:

case Hero.HITBIRD {

(assuming the class name is Hero). (假设班级名称为Hero)。

Note that you should probably replace these constants by an enum: 请注意,您可能应该用枚举替换这些常量:

public enum HeroDeathCause {
    FELL_OFF_SCREEN, HIT_BIRD, SQUASHED
}

This is more type-safe, and more self-descriptive. 这是更安全的类型,并且更具自我描述性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM