1糖果 - 2006-8-17 9:27:00
在C++中,怎么实现按一个键然后根据按键进行操作?
比如我按一个X,就输出"你按的是X"
按一个Y,就输出"你按的是Y"
天下奇才 - 2006-8-17 10:23:00
可以直接接用C函数,getch或者getchar
好像在stdlib.h中
我是天心飞仙 - 2006-8-17 12:10:00
<io.h>&<stdlib.h>
getchar();
1糖果 - 2006-8-17 13:14:00
#include "stdio.h"
#include "conio.h"
int main(void)
{
char key;
key=getch();
if(key="a"){
printf("你好帅!\n");
}
else{
printf("你好笨!\n");
}
}
运行时报错
1糖果 - 2006-8-17 13:14:00
再问一个.void main()和int main(void)不相同吗?
1糖果 - 2006-8-17 20:39:00
把#include "stdio.h"
#include "conio.h"
int main(void)
{
char key;
key=getch();
if(key="a"){
printf("你好帅!\n");
}
else{
printf("你好笨!\n");
}
}
改成
#include "stdio.h"
#include "conio.h"
int main(void)
{
char Key;
int key;
Key=getch();
key=Key;
switch(key)
{
case 97:
printf("你好帅!\n");
break;
case 98:
printf("厉害\n");
break;
default:
printf("顶\n");
}
}
就行了,为什么?难道IF语句用错了吗?
天下奇才 - 2006-8-18 15:48:00
| 引用: |
【1糖果的贴子】把#include "stdio.h" #include "conio.h" int main(void) { char key; key=getch(); if(key="a"){ printf("你好帅!\n"); } else{ printf("你好笨!\n"); } } 改成 #include "stdio.h" #include "conio.h" int main(void) { char Key; int key; Key=getch(); key=Key; switch(key) { case 97: printf("你好帅!\n"); break; case 98: printf("厉害\n"); break; default: printf("顶\n"); } } 就行了,为什么?难道IF语句用错了吗? ……………… |
if里面的改成key=='a'
© 2000 - 2026 Rising Corp. Ltd.