C++ C++;猜谜游戏 #包括 #包括 #包括 使用名称空间std; int main(); 无效菜单(); 无效游戏(); 再次无效(); int main() { //为随机数生成器设定种子 srand(时间(0)); 菜单(); 返回0; } //菜单,以便继续播放或关闭程序 无效菜单() { 整数选择=0; 布尔播放=真; 玩的时候 { 我可以看到多个问题: 游戏结束时,您缺少结尾“}”,再次出现函数 在后一种情况下,返回是可选的,并且应该在末尾有一个“;” 您正在将“decision”与单位化变量y、y、n和n进行比较……我猜您希望将其值与字符“y”、“y”、“n”、“n”进行比较 “else if(decision!=y,y | | n,n)”不是有效的语法,在这种情况下,您已经只使用了一个“else” 在“再次作废()”的末尾有一个额外的“;” 您正在尝试使用“void PlayGame();”调用函数 程序所采用的路径不干净,并且您正在执行不需要的递归,最好返回一个值

C++ C++;猜谜游戏 #包括 #包括 #包括 使用名称空间std; int main(); 无效菜单(); 无效游戏(); 再次无效(); int main() { //为随机数生成器设定种子 srand(时间(0)); 菜单(); 返回0; } //菜单,以便继续播放或关闭程序 无效菜单() { 整数选择=0; 布尔播放=真; 玩的时候 { 我可以看到多个问题: 游戏结束时,您缺少结尾“}”,再次出现函数 在后一种情况下,返回是可选的,并且应该在末尾有一个“;” 您正在将“decision”与单位化变量y、y、n和n进行比较……我猜您希望将其值与字符“y”、“y”、“n”、“n”进行比较 “else if(decision!=y,y | | n,n)”不是有效的语法,在这种情况下,您已经只使用了一个“else” 在“再次作废()”的末尾有一个额外的“;” 您正在尝试使用“void PlayGame();”调用函数 程序所采用的路径不干净,并且您正在执行不需要的递归,最好返回一个值,c++,C++,我会这样做: #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main(); void Menu(); void PlayGame(); void Again(); int main() { // Seed the random number generator srand(time(0)); Menu(); return 0; } //

我会这样做:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main();
void Menu();
void PlayGame();
void Again();

int main()
{
// Seed the random number generator
srand(time(0));
Menu();
return 0;
}

// Menu so can keep playing or close the program
void Menu()
{
int selection = 0;
bool playing = true;

while (playing)
{
    cout << "Guess Number Game\n\n";
    cout << "Menu\n\n";
    cout << "1) Play Game\n";
    cout << "2) Exit\n\n";
    cout << "Enter your selection: ";
    cin >> selection;
    cin.ignore();
    cout << endl;

    if (selection == 1)
       PlayGame();
    else if (selection == 2)
        playing = false;
    else if (selection != 1 || 2) //lets the user know why they are going        back to the menu
        cout << "Try again, choose 1 or 2" << endl;
  }

}


void PlayGame()
{
int RandomNumber = rand() % 50 + 1; // to set the random number between 1-50
int attempts = 15; // sets attempts at guessing the number to 15
int guess;
const int HighestNum = 50;
const int LowestNum = 1;

cout << "Guess the random number between 1 and 50!\n";

while (true)
{
    cout << "You have " << attempts << " attempts remaining\n\n"; // so the      user can keep track of attempts
    cout << "Enter a number: ";
    cin >> guess;
    cin.ignore();
    cout << '\n';

    if (guess == RandomNumber)
    {
        cout << "Congratulations, you won: " << RandomNumber << "!";
        cout << "\n\n" << endl;
        cout << "Would you like to play again y or n?" << endl;
        cin.get ();
        void Again();
    }
    else if (guess < RandomNumber) // to satisfy requirements of showing user whether the guess is too low
    {
        cout << "That guess is too low!\n" << endl;
    }
    else if (guess > RandomNumber)// to satisfy requirements of showing user whether the guess is too high
    {
        cout << "That guess is too high!\n" << endl;
    }
    else if (guess > HighestNum || guess < LowestNum)
    {
        cout << "Guess must be lower than 50 and higher than 1, Try again"     << endl; //setup so guesses not within the range of 1-50 do not count against the 15 guesses
        cin.get();//remove this to count guesses that are outside the 1-50
    }
    attempts--;

    if (attempts == 0)
    {
        cout << "Sorry, no guesses remain. The random number was... " <<     RandomNumber << "!";//so the user can see the random number at the end of their attempts
        cout << "\n";
        cin.get();
        void Again();
    }
}
void Again();
{
int decision = 0;
bool TryAgain = true;
char y;
char Y;
char n;
char N;

while (TryAgain)
{
    cout << "Y) Play Again\n";
    cout << "N) Exit\n\n";
    cout << "Enter your selection: ";
    cin >> decision;
    cin.ignore();
    cout << endl;

    if (decision == y || decision == Y)
    {
       PlayGame();
    }
    else if (decision == n || decision == N)
    {
        TryAgain = false;
    }
    else if (decision != y, Y || n, N) //lets the user know why they are going back to the menu
    {
        cout << "Try again, choose y or n" << endl;
    }
}
return
if (attempts == 0) {
    cout << "Sorry, no guesses remain. The random number was... " << RandomNumber 
        << "!";//so the user can see the random number at the end of their attempts
    cout << "\n";
    cin.get();
    void Again();
}
#包括
#包括
#包括
使用名称空间std;
虚空游戏()
{
const int HighestNum=50;
const int LowestNum=1;
int RandomNumber=LowestNum+rand()%HighestNum;

cout首先,您应该解决Alde提到的问题。接下来是
while(true)
语句的问题?您可能应该将其替换为
while(truments!=0)
,并设置:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


void PlayGame()
{
    const int HighestNum = 50;
    const int LowestNum = 1;
    int RandomNumber = LowestNum + rand() % HighestNum;

    cout << "Guess the random number between " << LowestNum << " and " << HighestNum << "!\n\n";

    int attempts = 15;
    while (attempts > 0)
    {
        int guess;
        cout << "You have " << attempts << " attempt" << (attempts > 1 ? "s" : "") << " remaining\n";

        cout << "Enter a number: ";
        cin >> guess;
        cout << endl;

        if (guess < LowestNum || guess > HighestNum)
        {
            cout << "Guess must be higher than " << LowestNum << " and be lower than " << HighestNum << ". Try again!\n"  << endl;
            continue;
        }

        if (guess == RandomNumber)
        {
            cout << "Congratulations, you won!\n\n"  << endl;
            return;
        }
        else if (guess < RandomNumber)
        {
            cout << "That guess is too low!\n" << endl;
        }
        else if (guess > RandomNumber)
        {
            cout << "That guess is too high!\n" << endl;
        }

        --attempts;
    }

    cout << "Sorry, no guesses remain. The random number was... " << RandomNumber << "!\n\n";
}

bool Menu()
{
    cout << "Guess Number Game\n\n";

    cout << "Menu\n\n";

    cout << "1) Play Game\n";
    cout << "2) Exit\n\n";

    cout << "Enter your selection: ";
    int selection = 0;

    while (true)
    {
        cin >> selection;
        cout << endl;

        if (selection == 1)
            return true;
        else if (selection == 2)
            return false;
        else
            cout << "Try again, choose 1 or 2: ";
    }
}

bool Again()
{
    cout << "1) Play Again\n";
    cout << "2) Exit\n\n";

    cout << "Enter your selection: ";
    int selection = 0;

    while (true)
    {
        cin >> selection;
        cout << endl;

        if (selection == 1)
        {
            return true;
        }
        else if (selection == 2)
        {
            return false;
        }
        else
        {
            cout << "Try again, choose 1 or 2: ";
        }
    }
}

int main()
{
    srand(time(0));

    bool play = Menu();
    if (play)
    while (true)
    {
        PlayGame();

        if (!Again())
            break;
    }

    return 0;
}

谢谢,再次使用菜单设置播放会更有意义。我不确定是否可以在void中调用void函数。我刚刚开始学习各种类型的函数。再次感谢。谢谢。我需要int reviewGuess(int,int)若要获取返回值,请包含一个switch语句以给出每个值的响应。基本上,返回值0必须=“You win!!”1“您的选择高于randomNumber”-1“您的选择低于randomNumber”但是在集成int-reviewGuess(int-randomNumber,int-userChoice){int x如果(randomNumber==userChoice)x=0;如果(randomNumber>userChoice)x=1;如果(randomNumberint reviewGuess(int randomNumber,int userChoice) { if(randomNumber == userChoice) return 0; if(userChoice > 50) return 1; if(userChoice < 1) return -1; }