三号lotttery猜谜游戏C ++

时间:2016-02-06 15:05:17

标签: c++ if-statement random

过去几天我一直坚持这个问题,这是我的最后一招。任何输入都表示赞赏!提前谢谢。

要求是创建一个程序,生成三个随机数,并允许用户猜测三个数字,为​​不同的匹配提供奖励。目前,该程序仅识别一个匹配,如果有多个匹配则不会显示。

#include <iostream>
#include <ctime>   // Needed for actual random number
#include <cstdlib>  

using namespace std;


int main()
{   
    int guess1, guess2, guess3;
    int random1, random2, random3;
    int exactCount = 0;
    int inexactCount = 0;

    /// initialize random seed /// 
    srand(time(NULL));  

    /// Generate three random numbers /// 
    random1 = rand() % 9 + 1;
    random2 = rand() % 9 + 1;
    random3 = rand() % 9 + 1;

    /// Accept three guesses ///
    cout << "Guess a number between 1 and 9: ";
    cin >> guess1;
    cout << "Guess another number between 1 and 9: ";
    cin >> guess2;
    cout << "Guess another number between 1 and 9: ";
    cin >> guess3;

    /// Test random numbers ///
    cout << "Number 1: " << random1 << endl << "Number 2: " << random2 << endl << "Number 3: " << random3 << endl;

    system("PAUSE");

    /// Count number of exact and inexact matches /// 
    if (guess1 == random1){
        exactCount++;
    }
    else if (guess1 == random2 || guess1 == random3){
        inexactCount;
    }
    if (guess2 == random2)
    {
        exactCount++;
    }
    else if (guess2 == random1 || guess2 == random3){
        inexactCount;
    }
    if (guess3 == random3){
        exactCount++;
    }
    else if(guess3 == random1 || guess3 == random2){
        inexactCount++;
    }

    int matches = exactCount + inexactCount;

    /// Check counts and display winnings /// 
    if (exactCount == 3){
        cout << "JACKPOT!!! Three matching in a row! You win $100!!!";
    }
    else
    {
        if (matches == 0) cout << "No matches at all, you lose!" << endl;
        else if (matches == 3) cout << "Three matching not in order! You win $50!" << endl;
        else if (matches == 2) cout << "Two matching numbers! You win $20!" << endl;
        else if (matches == 1) cout << "One matching number! You win $10!" << endl;
    }
    system("pause");
}

1 个答案:

答案 0 :(得分:0)

此代码应该可以正常工作!如果你想增加随机数的基数,我在unordered_set中添加了:)

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <unordered_set>
using std::unordered_set;
#include <cstdlib>
#include <time.h>
#include <array>
using std::array;

int main() {
    srand(time(NULL));
    unordered_set<int> set_of_random_values;

    // insert 3 random values into the set of integers
    for (int i = 0; i < 3; ++i) {
        set_of_random_values.insert(rand() % 9 + 1);
    }

    // now ask the user for input
    array<int, 3> guesses;
    for (auto& guess : guesses) {
        cout << "Enter a guess ";
        cin >> guess;
    }

    // print the 3 values
    cout << "The three values were : ";
    for (auto integer : set_of_random_values) {
        cout << integer << ' ';
    } cout << endl;

    int matches = 0;
    for (auto integer : guesses) {
        if (set_of_random_values.find(integer) != set_of_random_values.end()) {
            ++matches;
        }
    }

    if (matches == 0) cout << "No matches at all, you lose!" << endl;
    else if (matches == 3) cout << "Three matching not in order! You win $50!" << endl;
    else if (matches == 2) cout << "Two matching numbers! You win $20!" << endl;
    else if (matches == 1) cout << "One matching number! You win $10!" << endl;

    return 0;
}

正如@PaulMackinzie指出的那样,我只是想说你应该像我在上面的例子中那样使用循环,并尝试不重复代码!