gpt4 book ai didi

c++ - 立方根猜谜游戏

转载 作者:太空狗 更新时间:2023-10-29 23:52:06 25 4
gpt4 key购买 nike

我正在制作一个简单的立方根猜谜游戏,其中生成一个随机数并显示它的立方体,然后用户输入立方根是什么。这是我的程序:

int main()
try {
int max, min;
max = 99; min = 1; // only cubes of 1-99 are displayed

// display the title
cout << "\n\t\t\t\tCube Root Game" << endl;
cout << "\t\t\t\t=============\n" << endl;
srand(time(0)); // seed for random number generator

// display 10 numbers for the user to guess the cube root
for (int i = 0; i < 10; i++) {
int answer; // answer inputted by the user
int temp = rand() % (max - min) + min; // random number
int t3 = temp * temp * temp; // cube of the random number

cout << "\tEnter the cube root for " << t3 << " : ";
cin >> answer;

if (answer == t3) {
cout << "\tCorrect answer!\n" << endl;
}
else {
cout << "\tIncorrect answer\n" << endl;
}
}
keep_window_open("q");
}
catch (runtime_error& e) {
cerr << "Error: " << e.what() << endl;
keep_window_open("q");
return 1;
}
catch(...) {
cerr << "Unexpected error.\n";
}

问题是,当我正确输入立方根时,它总是说它不正确,但 if 比较对我来说似乎没问题,所以我不知道出了什么问题。

最佳答案

if (answer == t3) 

你不是这个意思吗:

if (answer == temp) 

(您希望用户猜测根,而不是立方体,对吗?):-)

关于c++ - 立方根猜谜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17389220/

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