键盘输入和不同的键有不同的反应
Keyboard input and different keys having different response
下面的代码假定从用户那里获得 W、A、S 或 D 的键盘输入。按下每个键时,需要在屏幕上显示不同的消息。我假设使用 if 语句来完成此任务。目前,错误的响应出现在错误的输入中。
我目前得到的结果是:
- 输入 W = 字符向上和字符向右
- 输入 S = 角色向下,角色向右
- 输入 A = 角色向左,角色向右
- 输入 D = 角色向右
我想要的结果如下:
- 输入 W = 角色向上
- 输入 S = 字符向下
- 输入 A = 字符向左
- 输入 D = 角色向右
谢谢
#include <stdio.h>
int main()
{
char keyInput[2];
//Instructions for user to follow
printf("Controls: \n");
printf("W = UP: \n");
printf("A = LEFT \n");
printf("S = DOWN \n");
printf("D = RIGHT \n");
printf("\nWhich direction do you want to go?");
scanf("%s", keyInput);
//choices
if (*keyInput == 'W')
{
printf("Character going UP\n");
}
else if (*keyInput == 'A')
{
printf("Character going LEFT\n");
}
else if (*keyInput == 'S')
{
printf("Character going DOWN\n");
}
else if (*keyInput == 'D');
{
printf("Character going RIGHT\n");
}
return 0;
}
这一行:
else if (*keyInput == 'D');
不应以分号结尾
下面的代码假定从用户那里获得 W、A、S 或 D 的键盘输入。按下每个键时,需要在屏幕上显示不同的消息。我假设使用 if 语句来完成此任务。目前,错误的响应出现在错误的输入中。
我目前得到的结果是:
- 输入 W = 字符向上和字符向右
- 输入 S = 角色向下,角色向右
- 输入 A = 角色向左,角色向右
- 输入 D = 角色向右
我想要的结果如下:
- 输入 W = 角色向上
- 输入 S = 字符向下
- 输入 A = 字符向左
- 输入 D = 角色向右
谢谢
#include <stdio.h>
int main()
{
char keyInput[2];
//Instructions for user to follow
printf("Controls: \n");
printf("W = UP: \n");
printf("A = LEFT \n");
printf("S = DOWN \n");
printf("D = RIGHT \n");
printf("\nWhich direction do you want to go?");
scanf("%s", keyInput);
//choices
if (*keyInput == 'W')
{
printf("Character going UP\n");
}
else if (*keyInput == 'A')
{
printf("Character going LEFT\n");
}
else if (*keyInput == 'S')
{
printf("Character going DOWN\n");
}
else if (*keyInput == 'D');
{
printf("Character going RIGHT\n");
}
return 0;
}
这一行:
else if (*keyInput == 'D');
不应以分号结尾