首页 > 解决方案 > 重置总变量

问题描述

我正在开发一个类似二十一点的程序。我最初生成一对随机卡片并存储数字的总数。如果用户愿意,他或她可以选择再次播放。当我编译程序并选择再次播放选项时(在第一个提示中输入“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;
}

谢谢!

标签: c++

解决方案


问题解决: , 的声明和初始化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;
}


推荐阅读