gpt4 book ai didi

C++:我应该如何重新排列这段代码,以便循环检查这两种情况?

转载 作者:太空宇宙 更新时间:2023-11-04 13:25:56 25 4
gpt4 key购买 nike

我正在用 C++ 编写老虎机程序作为类作业。该程序应该占您总信用额的一定百分比(从 1000.00 开始)并生成三个随机数来表示三个插槽号。如果所有三个数字都匹配,则用户获胜。只要用户还有积分,游戏就会继续进行,程序应该始终询问用户是否要继续。

我基本上已经获得了程序运行所需的一切,但我的问题是,每当我玩游戏并获得匹配时,它不会提示用户输入更多点数。相反,它只是结束程序。我认为这与我编写 while 循环的方式有关,因为该条件仅适用于数字不匹配的情况,并且由于它们不匹配,因此无法检查循环中的第一个 if 语句.我尝试使用 do-while,但情况相同。我如何重新排列代码,以便即使用户赢了,它也会提示用户再次下注?

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

using namespace std;

/* Start: Do Not Change */
const int MAX = 5;
const int MIN = 1;

// This function takes tree ints as parameters
// and sets them to a random number
int spin(int & n1, int & n2, int & n3)
{
n1 = rand()%(MAX-MIN)+MIN;
n2 = rand()%(MAX-MIN)+MIN;
n3 = rand()%(MAX-MIN)+MIN;
}
/* End: Do Not Change */

int main()
{
int slot1, slot2, slot3;
double percent;
double credits = 1000.00;
char keepPlaying;

srand (time(NULL));

cout << "Welcome to the slots, you have 1,000.00 credits.\n"
<< "What percentage of credits do you want to bet(0-1)? ";

cin >> percent;
spin(slot1, slot2, slot3); // At this point slot1-3 all have a random value between 1 and 5
//spin function
cout << "Slot value = " << slot1 << "-" << slot2 << "-" << slot3 << endl;

while (slot1 != slot2 || slot1 != slot3 || slot2 != slot3)
{
if (slot1 == slot2 && slot1 == slot3 && slot2 == slot3)
{
cout << "\tYou won " << fixed << setprecision(2) << credits * percent << " credits!\n";
credits += (credits * percent);

cout << "Continue (y/n)? ";
cin >> keepPlaying;

if (keepPlaying == 'y')
{
cout << "You have " << credits << " credits left.\n";
cout << "What percentage of credits do you want to bet(0-1)? ";

cin >> percent;

spin(slot1, slot2, slot3);
cout << "Slot value = " << slot1 << "-" << slot2 << "-" << slot3 << endl;

continue;
}

else if (keepPlaying == 'n')
{
cout << "You are leaving with " << credits << " credits." << endl;
return 0;
}
}

else
{
cout << "\tSorry, you lost " << fixed << setprecision(2) << credits * percent << " credits." << endl;
credits -= (credits * percent);

cout << "Continue (y/n)? ";
cin >> keepPlaying;

if (keepPlaying == 'y' && credits != 0)
{
cout << "You have " << credits << " credits left.\n";
cout << "What percentage of credits do you want to bet(0-1)? ";

cin >> percent;

spin(slot1, slot2, slot3);
cout << "Slot value = " << slot1 << "-" << slot2 << "-" << slot3 << endl;

continue;
}

else if (keepPlaying == 'y' && credits == 0)
{
cout << "Get out of here bum!" << endl;
return 0;
}

else if (keepPlaying == 'n')
{
cout << "You are leaving with " << fixed << setprecision(2) << credits << " credits." << endl;
return 0;
}
}
}

return 0;
}`

最佳答案

试一试:

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

using namespace std;

/* Start: Do Not Change */
const int MAX = 5;
const int MIN = 1;

// This function takes tree ints as parameters
// and sets them to a random number
void spin(int & n1, int & n2, int & n3)
{
n1 = rand()%(MAX-MIN)+MIN;
n2 = rand()%(MAX-MIN)+MIN;
n3 = rand()%(MAX-MIN)+MIN;
}
/* End: Do Not Change */

// How many credits the user has to play with
double credits = 1000.00; // 1000 is the starting point

void doGame(){
int slot1, slot2, slot3;
double percent;
double dCreditsWagered;
cout << "What percentage of credits do you want to bet(0-1)? ";

cin >> percent;
dCreditsWagered = credits*percent;

spin(slot1, slot2, slot3); // At this point slot1-3 all have a random value between 1 and 5
//spin function
cout << "Slot value = " << slot1 << "-" << slot2 << "-" << slot3 << endl;
// Check if all slots are equal:
if( (slot1 == slot2) && (slot1 == slot3)){
// User won!
cout << "\tYou won " << fixed << setprecision(2) << dCreditsWagered << " credits!" << endl;
credits += dCreditsWagered;
} else {
// User lost :(
cout << "\tSorry, you lost " << fixed << setprecision(2) << dCreditsWagered << " credits." << endl;
credits -= dCreditsWagered;
}
}

int main(){
char keepPlaying;

srand (time(NULL));

cout << "Welcome to the slots, you have 1,000.00 credits." << endl;

do{
doGame();
cout << "Continue (y/n)? ";
cin >> keepPlaying;
if (keepPlaying == 'y' && credits > 0) {
// User would like to play another round
cout << "You have " << credits << " credits left." << endl;

} else if(keepPlaying=='y' && credits <= 0){ // credits should never be < 0, but hey, doesn't hurt to check, right?
// User does not have enough credits to continue playing:
cout << "Get out of here you bum!" << endl;
break;
}else if (keepPlaying == 'n') {
// User would like to quit
cout << "You are leaving with " << credits << " credits." << endl;
break;
}

}while(true);

return 0;
}

我将您的实际游戏逻辑移到了名为 doGame() 的函数中。运行时,游戏会进入无限循环,执行 doGame() 然后检查用户是否想继续玩。如果用户想继续玩并且他们还有剩余的积分,循环将重新开始(因此 doGame() 将再次被调用。)如果用户选择退出或者如果用户不在学分并且他们尝试再次玩,代码将跳出 do-while 循环。

我还没有测试过这段代码,所以如果您发现错误,请告诉我,以便我进行修复。

编辑:刚刚编译和测试。我发现了两个错误。 #1,你的 int spin(int &n1, int &n2, int &n3) 函数被声明为返回一个 int,但它没有返回任何东西。 #2,我在 }while(true) 之后少了一个分号(语法错误)。

除了这两个错误之外,代码似乎按预期执行。理想情况下,应该添加一个名为 bool keepPlaying() 的函数,它会提示用户继续播放,然后验证用户的输入是 y 还是 n(如果他们输入了其他任何内容,则再次提示他们),但我会把它留给你 :P

关于C++:我应该如何重新排列这段代码,以便循环检查这两种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33336270/

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