我是新手,求解关于switch的用法

2025-05-14 00:25:45
推荐回答(3个)
回答1:

switch()后边不要有;
复制你定义的intUserChoice替换掉switch中的(intUserChoice),
int _tmain(int argc, _TCHAR* argv[])
{
int intComputerChoice;
int intUserChoice;
srand(unsigned(time(NULL)));
intComputerChoice = 1 + rand() % 3;
printf("Enter your choice (1—Rock; 2—Paper; 3—Scissors): ");
scanf("%d", &intUserChoice);
printf("My choice: ");
switch (intUserChoice)
{
case 1:
printf("Rock\n"); break;
case 2:
printf("Paper\n"); break;
case 3:
printf("Scissors\n"); break;
default:
printf("error\n");
}

return 0;
}

发现问题了 你的括号是中文输入法的括号()换成英文的()

回答2:

switch(intUserChoice)

后面不要加分号,如果加了分号的话,表示你的switch已经结束,后面的case找不到对应的switch就会报错。
还有,你的switch(intUserChoice)用的是全角的括号。必须要用半角的。

回答3:

 switch (intUserChoice);//去掉分号,去掉空格,改为switch(intUserChoice)

满意请采纳,若有疑问,请追问