如果语句不工作? [英] If statements not working?
本文介绍了如果语句不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在编程小白。我只是想问问有什么不妥以下code:
I am a noob at programming. I just wanted to ask what is wrong with the following code:
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语句,尽管它们都具有不同的条件?哪里不对?先谢谢了。
What is happening is that it is doing both of the if statements, even though they both have different conditions? What is wrong? Thanks in advance.
推荐答案
你混淆赋值运算符 =
与等于运算符 ==
。而不是这样写:
You're confusing the assignment operator =
with the equals operator ==
. Write this instead:
if (battlechoice == 4)
等。
某些C程序员使用尤达条件句,以避免无意中在这些情况下使用分配:
Some C programmers use "Yoda conditionals" to avoid accidentally using assignment in these cases:
if (4 == battlechoice)
例如,这将不能编译,捕捉错误:
For example this won't compile, catching the mistake:
if (4 = battlechoice)
这篇关于如果语句不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文