掷骰子游戏做while循环不满足退出条件
Posted
技术标签:
【中文标题】掷骰子游戏做while循环不满足退出条件【英文标题】:Game of Craps do while loop not meeting exit condition 【发布时间】:2019-10-29 15:59:24 【问题描述】:我正在尝试使用 C++ 模拟掷骰子游戏,其中对游戏进行多次迭代,并记录每场游戏的每场胜利、失败和掷骰数。
每场比赛的规则:
掷 2 个骰子并将结果相加。 2、3 或 12 = 输(1 卷),7 或 11 = 赢(1 卷)。如果两者都没有,则再次掷骰子,直到玩家掷出 7(输)或再次掷出原来的分数(赢)。
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<vector> // Allows use of vectors
double random(); // Returns random double between 0 and 1
int dice(double x); // Converts a double between 0 and 1 into an int between 1 and 6
int two_dice(int die_one, int die_two); // Sums 2 ints together, in this case dice rolls
int main()
// Sets the seed for rand using the current time, i.e. rand()
// will now always generate from a different starting point
std::srand(std::time(nullptr));
// Vectors to store the results of win/lose
std::vector<int> won;
std::vector<int> lose;
for (int i = 0; i < 10; i++)
int num_throws = 1;
// Roll two dice
double x = random(), y = random();
int die_one = dice(x);
int die_two = dice(y);
int roll = two_dice(die_one, die_two);
std::cout << "\nRoll " << i + 1 << " = " << roll << "\n";
if (roll < 4 || roll == 12) // Instant loss
lose.push_back(num_throws);
else if (roll == 7 || roll == 11) // Instant win
won.push_back(num_throws);
else // Begin looping rolls until a 7 or original roll is rolled again
int point = roll; // First roll
std::cout << "Point is " << point << "\n";
do
num_throws++;
double x = random(), y = random();
int die_one = dice(x);
int die_two = dice(y);
int roll = two_dice(die_one, die_two); // Roll dice again
std::cout << "Next roll = " << roll << "\n";
std::cout << "Point is " << point << "\n";
while (roll != 7 || roll != point);
if (roll == 7)
lose.push_back(num_throws);
std::cout << "lose\n";
else if (roll == point)
won.push_back(num_throws);
std::cout << "win\n";
else
std::cout << "\n\nERROR\n\n";
std::cout << "Number of throws was " << num_throws << "\n";
std::cout << "Number of throws for each win:\n";
for (auto i: won)
std::cout << i << "\n";
std::cout << "\nNumber of throws for each lose:\n";
for (auto i : lose)
std::cout << i << "\n";
return 0;
double random() // Returns random double between 0 and 1
double rand_num = rand() % 1000 + 1;
return rand_num / 1000;
int dice(double x) // Converts a double between 0 and 1 into an int between 1 and 6
double dice_roll = (x * 6) + 1;
return dice_roll;
int two_dice(int die_one, int die_two) // Sums 2 ints together, in this case dice rolls
return die_one + die_two;
在运行代码时,do-while 循环退出条件永远不会满足,即使滚动实际上 = 7 或点也是如此。它只是不断循环。我也尝试将 int 点更改为 const int 点,但都不起作用。有谁知道为什么会这样?
我知道这可能被过度评论了,但这是为了任何人都可以快速尝试阅读代码,并且我已经包含了很多打印语句,所以你最好尝试找出什么不起作用。
编辑:我的问题已经解决了,谢谢大家的回答!
【问题讨论】:
试着想一个数字会导致,例如,roll != 7 || roll != 9
为假。
试试while(roll != 7 && roll != point);
当我使用 && 时,它会立即退出循环,即执行 do 部分但随后退出而不管结果。
【参考方案1】:
问题出在while (roll != 7 || roll != point);
这一行。
假设points
不等于7;如果 roll 等于 7,则 roll != points
评估为 true,因此循环继续。反之亦然,如果roll == points
则roll != 7
为真,循环继续。
既然你在while循环之后测试结果,我建议使用break
statement:
do
num_throws++;
double x = random(), y = random();
int die_one = dice(x);
int die_two = dice(y);
int roll = two_dice(die_one, die_two); // Roll dice again
std::cout << "Next roll = " << roll << "\n";
std::cout << "Point is " << point << "\n";
if (roll == 7)
lose.push_back(num_throws);
std::cout << "lose\n";
break;
else if (roll == point)
won.push_back(num_throws);
std::cout << "win\n";
break;
while (true);
【讨论】:
以上是关于掷骰子游戏做while循环不满足退出条件的主要内容,如果未能解决你的问题,请参考以下文章