c++ - 重置总变量
问题描述
我正在开发一个类似二十一点的程序。我最初生成一对随机卡片并存储数字的总数。如果用户愿意,他或她可以选择再次播放。当我编译程序并选择再次播放选项时(在第一个提示中输入“n”),总数与之前生成的两个随机数相同。如何重新生成随机数并更新总变量?
到目前为止我的代码:
/*
Cortez Phenix
The 25th of January, 2021
CS10B, Mr. Harden
Assignment 2.1
This program uses loops and variables, allowing the user to play a blackjack-like game.
*/
#include <iostream>
#include <ctime>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
srand(static_cast<unsigned>(time(nullptr)));
string card_choice;
string repeat_choice;
int num_1 = rand()%10+1;
int num_2 = rand()%10+1;
int total = num_1 + num_2;
do{
do{
cout << "\nFirst Cards: " << num_1 << ", " << num_2;
cout << "\nTotal: " << total << "\n\n";
do{
cout << "Do you want another card? (y/n) ";
cin >> card_choice;
}
while (card_choice == "y" && (total += rand()%10+1) && cout << "Total: " << total << "\n\n" && (total != 21) && total < 21);
if (card_choice == "n"){
cout << "\nDo you want to play again? (y/n) ";
cin >> repeat_choice;
break;
}
if (total == 21){
cout << "Congratulations!\n";
total = num_1 + num_2;
}
if (total > 21){
cout << "Bust!\n";
total = num_1 + num_2;
}
}
while (repeat_choice == "y");
if (repeat_choice == "n"){
break;
}
}
while (repeat_choice == "y");
return 0;
}
谢谢!
解决方案
问题解决: , 的声明和初始化num1
应该num2
移到total
第一个do-while
循环中。
这是正确的代码,也带有编辑的缩进:
/*
Cortez Phenix
The 25th of January, 2021
CS10B, Mr. Harden
Assignment 2.1
This program uses loops and variables, allowing the user to play a blackjack-like game.
*/
#include <iostream>
#include <ctime>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
srand(static_cast<unsigned>(time(nullptr)));
string card_choice;
string repeat_choice;
do{
int num_1 = rand()%10+1;
int num_2 = rand()%10+1;
int total = num_1 + num_2;
do{
cout << "\nFirst Cards: " << num_1 << ", " << num_2;
cout << "\nTotal: " << total << "\n\n";
do{
cout << "Do you want another card? (y/n) ";
cin >> card_choice;
}
while (card_choice == "y" && (total += rand()%10+1) && cout << "Total: " << total << "\n\n" && (total != 21) && total < 21);
if (card_choice == "n"){
cout << "\nDo you want to play again? (y/n) ";
cin >> repeat_choice;
break;
}
if (total == 21){
cout << "Congratulations!\n";
total = num_1 + num_2;
}
if (total > 21){
cout << "Bust!\n";
total = num_1 + num_2;
}
}
while (repeat_choice == "y");
if (repeat_choice == "n"){
break;
}
}
while (repeat_choice == "y");
return 0;
}
编辑:这是完整的程序,带有文档-
/*
Cortez Phenix
The 26th of January, 2021
CS10B, Mr. Harden
Assignment 2.1
This program uses do-while loops, variables, if-then statements, and randomly generated numbers, allowing the user to play a blackjack-like game.
*/
#include <iostream>
#include <ctime>
#include <iomanip>
#include <string>
using namespace std;
int main() {
//Get a different sequence of random numbers each time they need to be generated.
//Initialize two string variables, later used to allow the user to draw another card or stop playing the game.
srand(static_cast < unsigned > (time(nullptr)));
string card_choice;
string repeat_choice;
//Main do-while loop that controls game.
do {
//Initialize and assign int variables of randomly generated numbers, 1-10. Store the total.
int num_1 = rand() % 10 + 1;
int num_2 = rand() % 10 + 1;
int total = num_1 + num_2;
//Display the initially-generated first cards and the total.
do {
cout << "\nFirst Cards: " << num_1 << ", " << num_2;
cout << "\nTotal: " << total << "\n\n";
//Choose new card or not. If yes, generate new number, then update and display the total, provided the total is not equal to or greater than 21.
do {
cout << "Do you want another card? (y/n) ";
cin >> card_choice;
}
while (card_choice == "y" && (total += rand() % 10 + 1) && cout << "Total: " << total << "\n\n" && (total != 21) && total < 21);
//If user does not want another card, prompt for a replay, and exit the loop.
if (card_choice == "n") {
cout << "\nDo you want to play again? (y/n) ";
cin >> repeat_choice;
break;
}
//If the total equals 21, the user wins. Prompt for a replay, and exit the loop.
if (total == 21) {
cout << "Congratulations!\n\n";
cout << "Do you want to play again? (y/n) ";
cin >> repeat_choice;
break;
}
//If the total is greater than 21, the user loses. Prompt for a replay, and exit the loop.
if (total > 21) {
cout << "Bust!\n\n";
cout << "Do you want to play again? (y/n) ";
cin >> repeat_choice;
break;
}
}
//Repeat the game, if the user chooses to.
while (repeat_choice == "y");
//Exit the game, if the user chooses to.
if (repeat_choice == "n") {
break;
}
}
//Repeat the game, if the user chooses to.
while (repeat_choice == "y");
return 0;
}
推荐阅读
- java - Spring Boot 更改覆盖异常响应
- python - matplotlib中的pcolormesh重新缩放轴
- r - 给定另一列的三个最小值,我需要找到一列中的值
- jenkins - 如何使用 CURL 将多级单选参数传递给詹金斯作业?
- python - 如何确定两个熊猫系列是否在给定的时间间隔内
- python - 如何将 dict 值添加到字段等于 dict 键的 mongodb 文档?
- javascript - 制作 100 多个计数器的最有效方法是什么?
- python - 为什么我的 PCA 对旋转和轴交换不是不变的?
- javascript - 我想在页面加载时自动从我的代码文件夹中获取一个文件到我的 js 文件
- python - 如何在 Selenium Python 中获取标签名称?