gpt4 book ai didi

c++ - 为什么 do - while only accepting pLife 条件?

转载 作者:行者123 更新时间:2023-11-28 02:49:01 26 4
gpt4 key购买 nike

循环的唯一方式是当玩家用尽生命时。该游戏应该允许玩家回答数学问题,他们回答直到他们或魔术师耗尽生命。所以在 do while 循环中,while 只在 pLife 用完后才工作,完全忽略 eLife,这是为什么呢?

do 
{
int Number = rand() % 20 + 1; //Desides the random number that will be used in the program
int aNumber = rand() % 20 + 1;
int rng = rand() % 3 + 1;
cout <<"Player Life Total = " <<pLife <<endl;
cout <<"Mathmagican Life Total = " <<eLife <<endl;

if (rng == 1)
{
cout <<"What is " <<Number <<"X" << aNumber <<"?\n\n";
cin >> answer;
if (answer == Number * aNumber)
{
cout <<"What!!! How is that possible you deflected my attack!\n\n";
cout <<"Ahh it hit me!! you hit me with my own magic! Thats not fair!\n\n";
eLife = eLife- 1;
}
else if (answer > Number * aNumber, answer < Number * aNumber)
{
cout <<"I told you that you couldn't defeat me!\n\n";
cout <<"Now die!";
pLife = pLife - 1;
}
}
else if (rng == 2)
{
cout <<"What is " <<Number <<"-" << aNumber <<"?\n\n";
cin >> answer;
if (answer == Number - aNumber)
{
cout <<"What!!! How is that possible you deflected my attack!\n\n";
cout <<"Ahh it hit me!! you hit me with my own magic! Thats not fair!\n\n";
eLife = eLife- 1;
}
else if (answer > Number - aNumber, answer < Number - aNumber)
{
cout <<"I told you that you couldn't defeat me!\n\n";
cout <<"Now die!";
pLife = pLife - 1;
}
}
else if (rng == 3)
{
cout <<"What is " <<Number <<"+" << aNumber <<"?\n\n";
cin >> answer;
if (answer == Number + aNumber)
{
cout <<"What!!! How is that possible you deflected my attack!\n\n";
cout <<"Ahh it hit me!! you hit me with my own magic! Thats not fair!\n\n";
eLife = eLife- 1;
}
else if (answer > Number + aNumber, answer < Number + aNumber)
{
cout <<"I told you that you couldn't defeat me!\n\n";
cout <<"Now die!";
pLife = pLife - 1;
}
}
}while (eLife > 0, pLife > 0);

if (eLife == 0)
{
cout <<"Oh no! I cant belive it... you... actualy... defeated me?!\n\n";
cout <<"NO!!! CURSE YOU!!!";
cout <<"You've Won!\n\n";
system ("pause");
return 0;
}
else if (pLife == 0)
{
cout <<"MWAHAHAHAHAHA!!!!! I told you i would win!\n\n";
cout <<"Game over";
system ("pause");
return 0;
}

最佳答案

以逗号分隔的表达式中的每个单独的表达式都将被计算,它们的副作用将发生。但是,整个逗号分隔表达式的值只是最右边表达式的结果。 因此,只有当 pLife > 0 返回 true 时,while 条件才计算为 true。

要更正此问题,请使用 &&|| 等 bool 运算符将其更改为单个表达式

关于c++ - 为什么 do - while only accepting pLife 条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23477953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com