具有char值的if语句 所以我试着用一个简单的C++写一个简单的基本游戏,当我尝试执行这个< /P> // global variabless const char UP = 'w', LEFT = 'a', DOWN = 's', RIGHT = 'd'; // player movement choices char playerMove; // goes with askPlayer void askPlayer() { char choice; cout << "Use the WASD keys to move: "; cin >> choice; int worked; do{ if (choice == 'w' || choice == 'W') { playerMove = UP; worked = 1; } else if (choice == 'a' || choice == 'A') { playerMove = LEFT; worked = 1; } else if (playerMove == 's' || playerMove == 'S') { playerMove = DOWN; worked = 1; } else if (playerMove == 'd' || playerMove == 'D') { playerMove = RIGHT; worked = 1; } else { cout << "Invalid entry." << endl; worked = 0; } } while (worked != 1); return; } //全局变量 const char UP='w',LEFT='a',DOWN='s',RIGHT='d';//球员动作选择 char playerMove;//与askPlayer一起使用 void askPlayer() { 字符选择; 选择; int工作; 做{ 如果(选项=='w'| |选项=='w') { playerMove=向上; 工作=1; } else if(choice='a'| | choice=='a') { playerMove=左; 工作=1; } else if(playerMove=='s'| | playerMove=='s') { playerMove=向下; 工作=1; } else if(playerMove=='d'| | playerMove=='d') { playerMove=右; 工作=1; } 其他的 { cout

具有char值的if语句 所以我试着用一个简单的C++写一个简单的基本游戏,当我尝试执行这个< /P> // global variabless const char UP = 'w', LEFT = 'a', DOWN = 's', RIGHT = 'd'; // player movement choices char playerMove; // goes with askPlayer void askPlayer() { char choice; cout << "Use the WASD keys to move: "; cin >> choice; int worked; do{ if (choice == 'w' || choice == 'W') { playerMove = UP; worked = 1; } else if (choice == 'a' || choice == 'A') { playerMove = LEFT; worked = 1; } else if (playerMove == 's' || playerMove == 'S') { playerMove = DOWN; worked = 1; } else if (playerMove == 'd' || playerMove == 'D') { playerMove = RIGHT; worked = 1; } else { cout << "Invalid entry." << endl; worked = 0; } } while (worked != 1); return; } //全局变量 const char UP='w',LEFT='a',DOWN='s',RIGHT='d';//球员动作选择 char playerMove;//与askPlayer一起使用 void askPlayer() { 字符选择; 选择; int工作; 做{ 如果(选项=='w'| |选项=='w') { playerMove=向上; 工作=1; } else if(choice='a'| | choice=='a') { playerMove=左; 工作=1; } else if(playerMove=='s'| | playerMove=='s') { playerMove=向下; 工作=1; } else if(playerMove=='d'| | playerMove=='d') { playerMove=右; 工作=1; } 其他的 { cout,c++,if-statement,char,C++,If Statement,Char,用户输入第一个值后,您永远不会提示输入另一个值: cin >> choice; // <== int worked; do { // .. } while (worked != 1); 用户输入第一个值后,您不会提示输入另一个值: cin >> choice; // <== int worked; do { // .. } while (worked != 1); 用户输入第一个值后,您不会提示输入另一个值: cin >>

用户输入第一个值后,您永远不会提示输入另一个值:

cin >> choice; // <==
int worked;

do {
    // ..
} while (worked != 1);

用户输入第一个值后,您不会提示输入另一个值:

cin >> choice; // <==
int worked;

do {
    // ..
} while (worked != 1);

用户输入第一个值后,您不会提示输入另一个值:

cin >> choice; // <==
int worked;

do {
    // ..
} while (worked != 1);

用户输入第一个值后,您不会提示输入另一个值:

cin >> choice; // <==
int worked;

do {
    // ..
} while (worked != 1);

您的输入在循环之外,您的变量
worked
未初始化(虽然这不是代码中的错误,但是初始化变量更干净),并且应该具有
bool
类型。整个代码可以通过
switch
语句简化:

void askPlayer()
{
    do {
        char choice;
        cout << "Use the WASD keys to move: ";
        cin >> choice;

        switch( choice ) {
            case 'w' : case 'W' :
                playerMove = UP;
                break;
            case 'a' : case 'A' :
                playerMove = LEFT;
                break;
            case 's' : case 'S' :
                playerMove = DOWN;
                break;
            case 'd' : case 'D' :
                playerMove = RIGHT;
                break;
            default:
                cout << "Invalid entry." << endl;
                continue;
        }
    } while( false );
    return;
}
void askPlayer()
{
做{
字符选择;
选择;
开关(选择){
案例“w”:案例“w”:
playerMove=向上;
打破
案例“a”:案例“a”:
playerMove=左;
打破
案例:案例:
playerMove=向下;
打破
案例“d”:案例“d”:
playerMove=右;
打破
违约:

cout您的输入在循环之外,您的变量
已工作
未初始化(虽然这不是代码中的错误,但初始化变量更干净),并且应该具有
bool
类型。整个代码可以通过
开关
语句简化:

void askPlayer()
{
    do {
        char choice;
        cout << "Use the WASD keys to move: ";
        cin >> choice;

        switch( choice ) {
            case 'w' : case 'W' :
                playerMove = UP;
                break;
            case 'a' : case 'A' :
                playerMove = LEFT;
                break;
            case 's' : case 'S' :
                playerMove = DOWN;
                break;
            case 'd' : case 'D' :
                playerMove = RIGHT;
                break;
            default:
                cout << "Invalid entry." << endl;
                continue;
        }
    } while( false );
    return;
}
void askPlayer()
{
做{
字符选择;
选择;
开关(选择){
案例“w”:案例“w”:
playerMove=向上;
打破
案例“a”:案例“a”:
playerMove=左;
打破
案例:案例:
playerMove=向下;
打破
案例“d”:案例“d”:
playerMove=右;
打破
违约:

cout您的输入在循环之外,您的变量
已工作
未初始化(虽然这不是代码中的错误,但初始化变量更干净),并且应该具有
bool
类型。整个代码可以通过
开关
语句简化:

void askPlayer()
{
    do {
        char choice;
        cout << "Use the WASD keys to move: ";
        cin >> choice;

        switch( choice ) {
            case 'w' : case 'W' :
                playerMove = UP;
                break;
            case 'a' : case 'A' :
                playerMove = LEFT;
                break;
            case 's' : case 'S' :
                playerMove = DOWN;
                break;
            case 'd' : case 'D' :
                playerMove = RIGHT;
                break;
            default:
                cout << "Invalid entry." << endl;
                continue;
        }
    } while( false );
    return;
}
void askPlayer()
{
做{
字符选择;
选择;
开关(选择){
案例“w”:案例“w”:
playerMove=向上;
打破
案例“a”:案例“a”:
playerMove=左;
打破
案例:案例:
playerMove=向下;
打破
案例“d”:案例“d”:
playerMove=右;
打破
违约:

cout您的输入在循环之外,您的变量
已工作
未初始化(虽然这不是代码中的错误,但初始化变量更干净),并且应该具有
bool
类型。整个代码可以通过
开关
语句简化:

void askPlayer()
{
    do {
        char choice;
        cout << "Use the WASD keys to move: ";
        cin >> choice;

        switch( choice ) {
            case 'w' : case 'W' :
                playerMove = UP;
                break;
            case 'a' : case 'A' :
                playerMove = LEFT;
                break;
            case 's' : case 'S' :
                playerMove = DOWN;
                break;
            case 'd' : case 'D' :
                playerMove = RIGHT;
                break;
            default:
                cout << "Invalid entry." << endl;
                continue;
        }
    } while( false );
    return;
}
void askPlayer()
{
做{
字符选择;
选择;
开关(选择){
案例“w”:案例“w”:
playerMove=向上;
打破
案例“a”:案例“a”:
playerMove=左;
打破
案例:案例:
playerMove=向下;
打破
案例“d”:案例“d”:
playerMove=右;
打破
违约:


您是否听说过
开关
?此外,您在使用
选项
播放移动
时似乎不一致。如果您使用不“工作”的
播放移动
(或
选项
)进入循环,请更正此问题,您希望如何取得进展?请使用
中的
std::tolower
,在比较之前将您的字母转换为小写。省去了大量的键入。请使用
bool
变量来表示
真/假
条件,而不是1或零。自20世纪60年代以来,时间发生了变化。您不需要分配
>playerMove
因为它是多余的。例如,
RIGHT=='d'
并替换,赋值变成
playerMove='d';
playerMove
已经等于'd',根据
if
语句的逻辑。@thomasatthews:
tolower()
有点棘手。它的参数是
int
,必须在
unsigned char
的范围内(或者它可以是
EOF
,通常是
-1
)。如果对纯
char
进行了签名,并且恰好有一个设置了符号位的
char
值,则调用
std::tolower()
有未定义的行为。使用
std::tolower((unsigned char)c)
(或者更合适的c++风格的类型转换)。听说过
switch
吗?另外,您在使用
选项
playerMove
时似乎不一致。如果您使用
playerMove
进入循环,请更正这一点(或
选项