gpt4 book ai didi

C++数学题

转载 作者:行者123 更新时间:2023-11-30 01:27:45 28 4
gpt4 key购买 nike

因此,在我的最终项目(使用从纸牌类继承的二十一点和扑克模拟器)中,我们必须跟踪用户的赌注和总金额。但是,在我的代码中,它做了非常奇怪的事情。例如:如果您的总资金为 1000000000 美元,您下注 100 并赢回 200,那么您的新总资金现在等于 199 美元

我的程序在执行此操作和不执行此操作之间来回切换。这太令人抓狂了,我不知道为什么会这样。以下是我的主要功能,以及处理每个扑克游戏的两个功能。如果有人认为需要更多代码来回答,尽管我很乐意包含类 header 和实现文件。感谢所有可能提供帮助的人!以下是我的主要功能,以及处理每个游戏的两个功能:

unsigned int handlePoker(unsigned int);
unsigned int handleBlackJack(unsigned int);
//main function:
//asks the user what game they want to play
//then calls a function for the appropriate
//game chosen
int main()
{//two choices:
//one for quitting the program
//the other for whichever game they want
char yesOrNo;
char choice;
unsigned int totalMoney;
cout<< "please enter a starting amount to bet with"<<endl;
cin>>totalMoney;
cout<<"would you like to play?"<<endl;
cout<<"enter 'y' for yes and 'n' for no"<<endl;
cin>>yesOrNo;
do{
//ask the user which game they want
cout<<"would you like to play poker or black jack?"<<endl;
cout<<"input '1' for poker and '0' for blackjack"<<endl;
cin>>choice;
if(choice == '1')
{
totalMoney = handlePoker(totalMoney);
}//end if
else if(choice == '0')
{
totalMoney = handleBlackJack(totalMoney);
}//end else if
else
{
cout<<"I'm sorry, the input you entered was invalid"<<endl;
cout<<"please try again"<<endl;
cin.clear();
}//end else
cout<<"would you like to try again?"<<endl;
cout<<"('y' for yes, or 'n' for no)"<<endl<<endl;
cin>>yesOrNo;
}while(yesOrNo == 'y' || yesOrNo == 'Y'); //end do while loop
return 0;
}//end int main


//handle poker:
//a void function which takes an
//unsigned integer value
//the function declares a "poker" object
//and uses it's void functions to sim a poker game
unsigned int handlePoker(unsigned int tot)
{
unsigned int multiply;
unsigned int betMonies;
unsigned int win;
poker round;
cout<<"how much do you want to bet?"<<endl;
cin>>betMonies;
//if the bet money entered was valid
// we begin playing
if(betMonies < tot)
{//big if begin
//ask if they want a better hand
round.betterHand();

//set the number we multiply your money by
multiply = round.rewardMoney();
//if multiply is 0
//then the user has lost this hand
//we inform them as such, and subtract
//their bet money from their total money
if(multiply == 0)
{
cout<<"I apologize, but you seem to have lost"<<endl;
cout<<"when you lose, your bet is subtracted"<<endl;
cout<<"your initial balance was: "<<tot<<endl;
//decrement the total
tot = (tot - betMonies);
cout<<"your new balance is: "<<tot<<endl;
}//end if
//if multiply is not 0 (assuming it's not negative
//because there's no way it could be)
//we tell them what they've won, and add it to
//their total money
else
{
win = (multiply*betMonies);
cout<<"your initial balance was: "<<tot<<endl;
cout<<"your win was"<<win<<endl;
//increment the total
tot = (win + tot);
cout<<"your new balance is "<<tot<<endl;
}//end else
}//big if end

//if the amount entered was not valid
//simply tell them, then run the loop again

else
{//else begin
cout<<"I'm sorry, that was not a valid amount of money"<<endl;
cout<<"please try again"<<endl;
}//end else
round.shuffleDeck();
return tot;

}//end handlePoker



//handle Black jack:
//a function returning an unsigned int
//that keeps track of the total money
//declares a black jack object
//and uses it's member functions to play black jack

unsigned int handleBlackJack(unsigned int tot)
{
blackJack play;
unsigned int reward;
unsigned int betMoolah;

//ask the user for the bet they want
cout<<"how much do you want to bet?"<<endl;
cin>>betMoolah;

//if the bet is less than the total passed by reference
//then we can start running the game
if(betMoolah < tot)
{

//print the hands dealt in the constructor
play.printHands();

//run the function that lets them hit or stay
//the function contains a do while loop
// so, no looping is required
play.hitOrStay();

//we then handle the reward
//which returns an integer type
reward = play.handleReward(betMoolah);

//prints dealer and player's hands fully
play.printHandGame();

//in one of the cases, reward is set to -1
//we use this here:
if(reward < 0 )
{
//if the reward is negative, then
//we subtract the bet money
//then we tell the user their new balance
cout<<"your balance was "<<tot<<endl;
cout<<"you have lost, so your bet is subtracted"<<endl;
tot = (tot-betMoolah);
cout<<"your new balance is: "<<tot<<endl;
}//end if



//if the reward is above 0, then we add the reward to the total
else if(reward > 0)
{
cout<<"your original balance was "<<tot<<endl;
cout<<"your winnings are: "<< reward<<endl;
tot = reward + tot;
cout<<"your new balance is: "<<tot<<endl;
}//end else

else
{
cout<<"you have lost no money"<<endl;
cout<<"your balance is still " <<tot<<endl;
}

}//end of the big if



//if the amount of money entered is above total money
//then the money entered isn't valid at all
else
{
cout<<"the amount of money you've entered is not valid"<<endl;
cout<<"please try again"<<endl;
}// end else
play.shuffleDeck();
return tot;

}//end handleBlackJack

最佳答案

我无法完全解释您看到的内容,但我确实注意到 unsigned int 的自由使用。特别是,在 handleBlackJack 中,您有:

unsigned int reward;

然后

reward = play.handleReward(betMoolah);

最后

if(reward < 0 )

如果奖励可以是负数,它不应该是一个unsigned int。正如其他人所建议的那样,在调试器中单步执行它并“关注金钱”。

关于C++数学题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8438351/

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