我正在编写一个简单的游戏,其中使用枚举CommandManager来存储有关可能的命令以及每个命令的作用的信息。该枚举的主要目的是能够打印出可用命令的菜单,以及用于检查输入并执行与该输入相关的操作。我的问题在于第二次使用,我使用 switch 语句来根据用户的输入确定用户想要执行的操作。当我尝试使用 Enum 的属性(通过 getter 方法)作为 case 标签时,出现编译错误。提供的错误消息是case expressions must be constant expressions。鉴于 的属性CommandManager被声明为最终的,我认为枚举的属性根本不能在 switch 语句中使用是正确的吗?如果是这样,为什么?
下面包含代码的简化版本,以防我这边出现错误。
方法代码:
void interpretInput() {
String command = input.getInput();
if (command.length() == 2) {
switch (command) {
case CommandManager.MAINMENU.getCommand(): goToMainMenu();
break;
case CommandManager.NEWGAME.getCommand(): startNewGame();
break;
case CommandManager.LISTGAMES.getCommand(): listSavedGames();
break;
case CommandManager.EXITGAME.getCommand(): exitGame();
break;
case CommandManager.HELPMENU.getCommand(): listAllCommands();
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
枚举代码:
public enum CommandManager {
NEWGAME("!n", "New game"),
MAINMENU("!m", "Go to main menu"),
EXITGAME("!q", "Exit Battleships"),
LISTGAMES("!g", …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的程序做案例菜单。我总是收到交叉初始化错误,我以前从未见过这个错误。也许有人可以向我解释我的代码出了什么问题。
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <fstream>
using namespace std;
#define N_CARS 1
struct car{
string model;
int year;
double price;
bool available;
}cars [N_CARS];
void menu();
void mainMenu();
void writeToFile(ofstream &outputFile , const car& p)
{
outputFile << p.model << " "
<< p.year << " "
<< p.price << " "
<< p.available<<"\n";
}
int choice1 = 0;
int main(int argc, char** argv) {
menu();
return 0;
}
void menu() {
do …Run Code Online (Sandbox Code Playgroud) int a = 0 , b = 0;
char* c = NULL;
int main(int argc , char ** argv){
c = argv[2];
a = atoi(argv[1]);
b = atoi(argv[3]);
switch(c){
case "+": printf(a+b);
break;
}
printf("\n\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个函数,在 python 中使用 PyQt4 单击按钮后会弹出一个消息框。我使用“sender()”来确定单击了哪个按钮,然后相应地设置弹出窗口的文本。该函数与“if 语句”完美配合。但是我想知道如何使用字典编写具有相同功能的函数(因为python中没有switch语句并且我的代码中有太多if语句)?
def pop_up(self):
msg = QtGui.QMessageBox()
msg.setIcon(QtGui.QMessageBox.Information)
sender = self.MainWindow.sender()
if sender is self.button1:
msg.setText("show message 1")
elif sender is self.button2:
msg.setText("show message 2")
elif sender is self.button3:
msg.setText("show message 3")
elif sender is self.button4:
msg.setText("show message 4")
elif sender is self.button5:
msg.setText("show message 5")
elif sender is self.button6:
msg.setText("show message 6")
.
.
.
.
.
elif sender is self.button36:
msg.setText("show message 36")
msg.exec()
Run Code Online (Sandbox Code Playgroud) 以下是 Dennis M Ritchie 的书 ANSI C 的摘录:
每种情况都由一个或多个整数值常量或常量表达式标记。
我无法想出一个开关盒的例子,其中我们有一个带有多个标签的开关盒。
任何说明上述属性的示例都会有所帮助。
我正在使用 JSON 架构jsonschema来验证 JSON 记录。这是一个示例架构。这里只有两个案例,但想象一下类似的场景,其中有一百个像这样布置的案例。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"oneOf": [
{
"type": "object",
"required": ["a", "b", "c"],
"properties": {
"a": {"type": "integer", "enum": [0]},
"b": {"type": "integer", "enum": [0, 2, 4, 6, 8]},
"c": {"type": "string", "enum": ["always the same"]}
}
},
{
"type": "object",
"required": ["a", "b", "c"],
"properties": {
"a": {"type": "integer", "enum": [1]},
"b": {"type": "integer", "enum": [1, 3, 5, 7, 9]},
"c": {"type": "string", "enum": ["always the same"]}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
关键问题是字段的重复"c" …
我需要将一些用户生成的字段映射到我正在开发的系统可以识别的内容。
为此,我们希望为用户提供一定的自由度,并为每个领域提供五个左右的选项。
到目前为止,我们有一个开关可以完成这项工作,但现在我们必须扩展开关,而且它会很大。不用说,这不是一种非常动态的方式。还有其他选择吗?
function findHeader(object) {
var title = object.toString().trim().toLowerCase()
switch (title) {
case 'name':
case 'idea':
case 'ide':
case 'ide navn':
case 'title':
case 'idea name':
title = 'name'
break
case 'beskrivelse':
case 'problemet':
case 'description':
case 'the problem':
case 'ide beskrivelse':
title = 'description'
break
case 'ejer':
case 'owner':
case 'opfinder':
case 'ide person':
case 'idea person':
case 'person':
title = 'owner'
break
case 'duedate':
case 'deadline':
case 'tidsfrist':
case 'sidste dato':
case 'dato':
case 'due date':
title …Run Code Online (Sandbox Code Playgroud) 我正在创建一个带有开关盒的简单菜单,因此根据写入的值它可以执行不同的操作,
\nvoid\nmenu_principal()\n{\n int opciones;\n\n printf ("Ingrese su eleccion \\n");\n\n scanf("%d", &opciones);\n\n switch(opciones) \n {\n case 1: \n \n int coord_x;\n int coord_y;\n \n printf("Ingrese las cordenadas de x \\n: ");\n scanf( "%d", &coord_x);\n \n printf("Ingrese las cordenadas de y \\n");\n scanf( "%d", &coord_y );\n \n break;\n \n \n case 0: \n \n break;\n \n \xe2\x80\xa6\n \n }\n return 0;\n\n}\nRun Code Online (Sandbox Code Playgroud)\n但它就是不起作用,我收到 和scanf, 的int coord_x错误coord_y。
在C++中,我可以做类似下面代码的事情吗?
在某个具体案例中,如果满足某些条件,是否可以跳转到另一个案例?
switch (tag) {
case 1: {
// do something
break;
}
case 2: {
if (/* condition */) {
// Can I somehow jump to case 1?
}
break;
}
}
Run Code Online (Sandbox Code Playgroud) 我找不到返回多个值(列)或仅返回一个小标题的方法case_when()。
input <- tibble(a = c(1, 2, 3))
input %>%
mutate(
case =
case_when(
a == 1 ~ tibble(x = "case1", y = "c1"),
a == 2 ~ tibble(x = "case2", y = "c2"),
a == 3 ~ tibble(x = "case3", y = "c3")
)
)
Run Code Online (Sandbox Code Playgroud)
如何根据某些正则表达式条件设置多个参数?
switch-statement ×10
c ×3
c++ ×2
dictionary ×1
enums ×1
goto ×1
if-statement ×1
java ×1
javascript ×1
jsonschema ×1
menu ×1
pyqt4 ×1
python ×1
r ×1
string ×1
tidyverse ×1
validation ×1