C++ 使用布尔条件c+;进行循环时无法退出+;
嗨,这是我的第一篇帖子。如果我没有遵守某些规则或惯例,我道歉。如果是这样,请让我知道 我有一个游戏,在一个while循环中运行,直到任何一个玩家达到分数限制,在这一点上,另一个玩家有最后一次(迭代)机会击败第一个玩家的分数。但是,在达到分数限制后,循环将继续运行,并且从不检查赢家C++ 使用布尔条件c+;进行循环时无法退出+;,c++,loops,while-loop,boolean,exit,C++,Loops,While Loop,Boolean,Exit,嗨,这是我的第一篇帖子。如果我没有遵守某些规则或惯例,我道歉。如果是这样,请让我知道 我有一个游戏,在一个while循环中运行,直到任何一个玩家达到分数限制,在这一点上,另一个玩家有最后一次(迭代)机会击败第一个玩家的分数。但是,在达到分数限制后,循环将继续运行,并且从不检查赢家 #include <iostream> #include <stdlib.h> #include <time.h> #include <string> using nam
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
int roll();
int playTurn(int);
int main(){
const int LIMIT = 5;
int whoseTurn = 1;
int pnts1 = 0;
int pnts2 = 0;
bool suddenDeath = false; //True when score limit is reached
while(!suddenDeath){
if(pnts1 >= LIMIT || pnts2 >= LIMIT){ //Limit was reached by previous player.
suddenDeath == true; //Next player has 1 turn to win
}
if(whoseTurn == 1){
pnts1 += playTurn(whoseTurn); //Play turn and tally points
whoseTurn = 2; //Swith player for next iteration
}
else if(whoseTurn == 2){
pnts2 += playTurn(whoseTurn);
whoseTurn = 1;
}
cout << "-------------------------------------" << endl //Display score
<< "Player 1 has " << pnts1 << " points" << endl
<< "Player 2 has " << pnts2 << " points" << endl
<< "-------------------------------------" << endl << endl;
};
if(pnts1 > pnts2)
cout << "Congratulations Player 1! You won with a score of: " << pnts1 << " - " << pnts2;
else if(pnts2 > pnts1)
cout << "Congratulations Player 2! You won with a score of: " << pnts2 << " - " << pnts1;
else if(pnts1 == pnts2)
cout << "A tie! What are the chances?";
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int roll();
int播放回合(int);
int main(){
常数int极限=5;
int whoseTurn=1;
int pnts1=0;
int pnts2=0;
bool suddenDeath=false;//达到分数限制时为True
而(!突然死亡){
如果(pnts1>=LIMIT | | pnts2>=LIMIT){//LIMIT是前一个玩家达到的。
suddenDeath==true;//下一个玩家有1回合获胜
}
如果(whoseTurn==1){
pnts1+=玩回合(whoseTurn);//玩回合和计分
whoseTurn=2;//为下一次迭代切换播放器
}
else if(whoseTurn==2){
pnts2+=播放回合(whoseTurn);
whoseTurn=1;
}
库特
是一个表示“比较这两个值”的表达式,然后将其丢弃。C语句42;
同样有效,也同样无用(a)
您希望分配该值,因此可以使用:
suddenDeath = true;
// ^
这实际上是更常见的if(a=0)
问题的另一端,人们在这里分配而不是比较
(a) 如果您想知道,为什么任何头脑正常的人都会允许将其应用到语言中,那么它实际上允许使用最少的代码实现一些强大的构造
而且,你很可能以前见过它。i++;
语句真是个怪兽。它是一个表达i
(你在这里扔掉了)的表达式,它的副作用是i
随后递增
suddenDeath = true;
是一个表示“比较这两个值”的表达式,然后将其丢弃。C语句42;
同样有效,也同样无用(a)
您希望分配该值,因此可以使用:
suddenDeath = true;
// ^
这实际上是更常见的if(a=0)
问题的另一端,人们在这里分配而不是比较
(a) 如果您想知道,为什么任何头脑正常的人都会允许将其应用到语言中,那么它实际上允许使用最少的代码实现一些强大的构造
而且,你很可能以前见过它。i++;
语句真是个怪兽。它是一个表达i
(你在这里扔掉了)的表达式,它的副作用是i
随后递增
suddenDeath = true;
使用单个=
进行分配。=
用于条件检查
使用单个=
进行赋值。=
用于条件检查。您的意思是suddenDeath=true;
而不是suddenDeath==true;
?您的意思是suddenDeath=true;
而不是suddenDeath==true;
?