我是编程的小伙子。我只是想问下面的代码有什么问题:
scanf("%i", &battlechoice);
printf("BCHOICE WAS:%i\n", battlechoice);
if (battlechoice=4) //fleeing
{
fleechance=rand() % 100;
if (fleechance <= 49)
{
printf("You attempt to flee...\n");
sleep(2000);
printf("Oh dear! You failed to flee! Gamover!\n");
printf("Thank you for playing! -Anthony\n");
sleep(7000);
exit(0);
}
else
{
printf("You succeeded in fleeing! You will be returned to town\nshortly...\n\n\n\n\n");
sleep(3000);
break;
}
} //end fleeing
else if (battlechoice=1) //attacking
{
//player damage gen
printf("You commence the attack...\n");
sleep(750);
damagemax = rand() % lvl * 1.4;
damageoutcome = damagemax + damagemin;
}
正在发生的是它正在执行两个if语句,即使它们都有不同的条件?怎么了?提前谢谢。
答案 0 :(得分:12)
您将赋值运算符=
与等号运算符==
混淆。写下这个:
if (battlechoice == 4)
等等。
一些C程序员使用“Yoda条件”来避免在这些情况下意外使用赋值:
if (4 == battlechoice)
例如,这将无法编译,捕获错误:
if (4 = battlechoice)
答案 1 :(得分:1)
你正在写作(battlechoice = 4) 用if(battlechoice == 4)
来纠正它因为'='和'=='运算符都不同
'='是赋值运算符,'=='是比较运算符
查看C http://www.tutorialspoint.com/cplusplus/cpp_operators.htm
中运算符的链接