简体   繁体   English

案例表达式必须是常量表达式

[英]Case expressions must be constant expression

Having some issues with the processing language where it says the case expressions must be constants, but I could have sworn they were. 在处理语言中有一些问题,它说case表达式必须是常量,但我可以发誓它们是。 I don't know what I am doing wrong here. 我不知道我在这里做错了什么。 Anyone have some tips? 有人有提示吗?

int gameState;
static int MENU = 0;
static int GAME = 1;
static int OPTIONS = 2;

void setup() {
  screenSizex = 960;
  screenSizey = 640;
  size(screenSizex, screenSizey);
  gameState = MENU;
}

void draw(){
  switch(gameState) {
    case MENU:
      //does menu stuff
      break;
    case OPTIONS:
      //does options stuff
      break;
    case GAME:
      //does game stuff
      break;
    default:
      break;
  }
}

void mousePressed() {
  if (//over some object) {
    gameState = GAME;
  }
  else if (//over some object) {
    gameState = OPTIONS;
  }
  else if (//over some object) {
    exit();
  }
}

static just makes fields belong to the class instead of an instance of the class. static只是使字段属于类而不是类的实例。 A static field can be modified at any time, so it isn't constant. 可以随时修改静态字段,因此它不是常量。 You need to make the fields final if you want them to be treated as constant values: 如果要将它们视为常量值,则需要将字段设为final

static final int MENU = 0;
static final int GAME = 1;
static final int OPTIONS = 2;

Marking the fields as static final means that they both exist at the class level (they don't belong to any particular instance of the class, nor do you need an instance to access the values) and cannot be modified after initialization (effectively making the values constant). 将字段标记为static final意味着它们都存在于类级别(它们不属于该类的任何特定实例,也不需要实例来访问值)并且在初始化后无法修改(有效地使值常数)。

However, I think this would really be a good place for you to use an enumeration type . 但是,我认为这对你来说真的是一个使用枚举类型的好地方。 Here's how I'd change the game state declaration: 这是我如何改变游戏状态声明:

GameState gameState;
enum GameState {
  MENU, GAME, OPTIONS
}

And here's what you'd have to do to get the rest of the code to work with that: 以下是您需要做的其余代码才能使用它:

void setup() {
  screenSizex = 960;
  screenSizey = 640;
  size(screenSizex, screenSizey);
  gameState = GameState.MENU;
}


void draw(){
  switch(gameState) {
    case MENU:
      //does menu stuff
      break;
    case OPTIONS:
      //does options stuff
      break;
    case GAME:
      //does game stuff
      break;
    default:
      break;
  }
}

void mousePressed() {
  if (/*over some object*/) {
    gameState = GameState.GAME;
  }
  else if (/*over some object*/) {
    gameState = GameState.OPTIONS;
  }
  else if (/*over some object*/) {
    exit();
  }
}

Basically all I had to change was adding a qualifying GameState. 基本上我只需更改一个符合条件的GameState. in front of all occurances of MENU , GAME and OPTION , with the exception of the ones in the case statements since the compiler can infer that those are values of GameState based on the type of the expression used in the switch . MENUGAMEOPTION的所有出现之前,除了case语句中的那些之外,因为编译器可以根据switch使用的表达式的类型推断出这些是GameState的值。 Using an enum instead of an int has the added advantage that it restricts the value of gameState to only the 3 valid values, instead of the entire range of integers. 使用enum而不是int具有额外的优点,即它将gameState的值限制为仅3个有效值,而不是整个整数范围。

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

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