gpt4 book ai didi

c++ - For循环——魔数(Magic Number)程序

转载 作者:行者123 更新时间:2023-11-28 05:28:11 25 4
gpt4 key购买 nike

我应该做哪些更改,以便此代码的用户可以猜测他们选择的魔数(Magic Number)的数量,并有三种不同的机会来猜测每个魔数(Magic Number)?我也对要更改的内容感到困惑,以便一旦用户正确猜测了魔数(Magic Number),魔数(Magic Number)就可以改变。

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

using namespace std;

int main()

{

int magic; // This is a random number between 1 and 20

int guess; // This is the guess number being attempted (up to 3 guesses)

int magicguesses; // This is the amount of magic numbers being guessed attempted

int i;

int number; // This is the number the user guessed

bool numberismagic = false; // This is the flag variable

unsigned int seed;

seed = time(NULL);

srand(seed);

magic = rand() % 20 + 1;

cout << "How many magic numbers would you like to guess at today?\n";

cin >> magicguesses;

for (i = 1; i < magicguesses + 1; i++)
{
cout << "This is trial number:" << i << endl;

for (guess = 1; (guess < 4) && (!numberismagic); guess++)
{
cout << "This is guess number:" << guess << endl;

cout << "Guess a number between 1 and 20:" << endl;

cin >> number;

while ((number < 1) || (number > 20))
{
cout << "Your guess is invalid; guess a number between 1 and 20 \n";

cin >> number;

cout << endl;
}

if (number == magic)
{
cout << "You have guessed the magic number correctly! \n";

numberismagic = true;
}
else
{
cout << "Sorry - you guessed incorrectly! \n";

if (number > magic)
cout << "Your guess is too high \n" << endl;
else
cout << "Your guess is too low \n" << endl;
}
}

if (number != magic)
cout << "The magic number is:" << magic << endl;
}

return 0;
}

最佳答案

我不确定您的第一个问题是什么,但是对于这个问题“我也对更改什么感到困惑,以便一旦用户正确猜出魔数(Magic Number)就可以更改魔数(Magic Number)”,您应该编辑变量 magic在第一个 for loop 中,因此魔数(Magic Number)会在用户猜对或尝试次数用完后发生变化。

for (i=1;i<magicguesses+1;i++)
{
//magic equals new random number
//the rest of your code
}

关于c++ - For循环——魔数(Magic Number)程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40098420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com