所以我想做一个投注游戏。玩家必须输入0到100之间的数字,并且它不能更高或更低。这就是我到目前为止所做的。
int Player_Money = 100;
int Computer_Money = 100;
int Check(int &b) { //b here would be Player_Money
int Player_Bet = 0;
bool Exit_Loop = false;
cout << "Please Enter A Number Between 0 and " << b;
while (!Exit_Loop) {
cin >> Player_Bet;
if (Player_Bet <= 0) {
cout << "The Amount That You Have Entered Is To Low.\n";
}
else if (Player_Bet > b) {
cout << "The Amount That You Have Entered Is Too High.\n";
}
if (Player_Bet > 0) {
if (Player_Bet < b) {
Exit_Loop = true;
if (Player_Bet = b) {
cout << "You Have Gone All In!\n";
Exit_Loop = true;
}
}
}
return Player_Bet;
}
}
当我尝试做Check(变量);这就是发生的事情。
如果它变低,它会说它为低,并退出循环。 如果它很高,它会说你输入的数字很高。然后退出。 如果它在范围内,它表示你全押,返回100,然后退出。
我想知道的是:
*如何正确地进行循环循环以及如何使函数返回正确的数字。
答案 0 :(得分:0)
如果它在范围内,它表示你全押,返回100,然后退出
if(Player_Bet = b)改变if(Player_Bet == b)因为=将b设置为Player_Bet而==是比较值。
如果您不希望它在具有范围内值之前退出,请移动返回Player_Bet;在while循环中
答案 1 :(得分:0)
首先检查Player_bet
是否高于0
且低于b
。检查之后是否等于b
。
int Player_Money = 100;
int Computer_Money = 100;
int Check(int &b) { //b here would be Player_Money
int Player_Bet = 0;
bool Exit_Loop = false;
cout << "Please Enter A Number Between 0 and " << b;
while (!Exit_Loop) {
cin >> Player_Bet;
if (Player_Bet <= 0) {
cout << "The Amount That You Have Entered Is To Low.\n";
}
else if (Player_Bet > b) {
cout << "The Amount That You Have Entered Is Too High.\n";
}
else if (Player_Bet > 0 && Player_Bet < b ) {
Exit_Loop = true;
}
else /* if (Player_Bet == b) */ {
cout << "You Have Gone All In!\n";
Exit_Loop = true;
}
}
return Player_Bet;
}
答案 2 :(得分:0)
我在您发布的代码段中看到的一个主要问题是,您的return
内的while loop
声明 。所以无论Player_Bet
的值是什么,总是会在第一次迭代结束时返回并结束方法....换句话说,如果永远不会实际循环。
答案 3 :(得分:0)
您的代码中存在以下几个问题:
这将更适合你。但请注意,做某事(真实)通常不是一个好主意。
while (true) {
cin >> Player_Bet;
if (Player_Bet <= 0) {
cout << "The Amount That You Have Entered Is To Low.\n";
} else if (Player_Bet > b) {
cout << "The Amount That You Have Entered Is Too High.\n";
} else {
if (Player_Bet == b) {
cout << "You Have Gone All In!\n";
}
return Player_Bet;
}
}