C++ 基本C++;骰子游戏
我这里有个问题,如果有人能帮我,我会很高兴的。这是我第一次使用这个程序,所以不要妄下判断C++ 基本C++;骰子游戏,c++,c++11,dice,C++,C++11,Dice,我这里有个问题,如果有人能帮我,我会很高兴的。这是我第一次使用这个程序,所以不要妄下判断 #include <cstdlib> #include <iostream> using namespace std; int throw1, throw2, throw3, throw4; int bet1 = 100; int bet2 = 300; int bet3 = 500; int bet=(bet1, bet2, bet3); int deposit; int acc
#include <cstdlib>
#include <iostream>
using namespace std;
int throw1, throw2, throw3, throw4;
int bet1 = 100;
int bet2 = 300;
int bet3 = 500;
int bet=(bet1, bet2, bet3);
int deposit;
int account;
int main(){
int count = 0;
while(count < 3){
cin>>deposit;
while(deposit>5000 || deposit<0){ //Makes sure so that my deposit is between 0-5000
cout<<"deposit failed"<<endl;
cin>>deposit;
}
account = deposit;
cout<<"You have deposited" <<deposit<<"Swedish Krona"<<endl;
cout<<"and you have this much cash on your account"<<account<<"Swedish Krona"<<endl;
if (konto>499){ //Makes sure so that i have the money to bet, and if i dont have the money, i can just put in more
cout<<"please place your bet"<<endl;
cout<<"bet1=100, bet2=300, bet3=500"<<endl;
cin>>bet1;
cin>>bet2;
cin>>bet3;
account = (deposit - bet);
cout<<"you have this much cash on your account"<<account<<"Swedish Krona"<<endl;
}
else if(account>299){
cout<<"please place your bet"<<endl;
cout<<"bet1=100, bet=300"<<endl;
cin>>bet1;
cin>>bet2;
account =(deposit - bet);
cout<<"you have this much cash on your account"<<account<<"Swedish Krona"<<endl;
}
else if(account>99){
cout<<"please place your bet"<<endl;
cout<<"bet1=100"<<endl;
cin>>bet1;
cout<<"you have placed your bet"<<bet<<"Swedish Krona"<<endl;
}
while (account<100 || deposit>5000){
cout<<"insufficient funds"<<endl;
cin>>deposit;
account=deposit;
}
{
cout<<"Throw dice"<<endl;
srand(time(0));
Throw1 = rand() % 6 + 1;
Throw2 = rand() % 6 + 1;
Throw3 = rand() % 6 + 1;
Throw4 = rand() % 6 + 1;
cout<<"You rolled"<<Throw1<<endl;
cout<<"You rolled"<<Throw2<<endl;
cout<<"Computer rolled"<<Throw3<<endl;
cout<<"Computer rolled"<<Throw4<<endl;
}
}
count++;
system ("pause");
}
#包括
#包括
使用名称空间std;
int throw1、throw2、throw3、throw4;
int bet1=100;
int bet2=300;
int bet3=500;
int bet=(bet1,bet2,bet3);
内部存款;
国际账户;
int main(){
整数计数=0;
而(计数<3){
cin>>矿床;
当(5000)的沉积物在代码下面,你的状态在代码下面,你的代码>代码>(int计数0,而(计数3)计数+++)< /> >看起来像< <代码> > <代码> > <代码> >。请再次检查C++的教科书/在线教程,关于如何编写正确的循环。
在您显示的代码中,在while
循环中,您没有修改count
变量-因此,如果循环前的count小于3,它将永远循环。代码的缩进确实有误导性。我已经冒昧地重新格式化了代码-现在您应该看到count++
语句实际上在主循环之外,而循环
当你想做固定次数的事情时,建议使用for
循环,这会让你更难忘记增量
int bet1 = 100;
int bet2 = 300;
int bet3 = 500;
int bet=(bet1, bet2, bet3)
最后一行的计算结果如下:100300500。用逗号分隔的表达式列表的结果将是最后一个值,即500。因此,您的下注变量将始终设置为500。您在循环外增加计数,因此它将始终为零。或者将其移动到循环内(正确的缩进是关键!)或者可以使用for
循环:
for (count = 0; count < 3; ++count) { ... }
for(count=0;count<3;++count){…}
一些建议
- 将存款提示(INSATTING)放入函数中
- 将下注提示放入函数中
- 在提示下注之前检查是否有足够的钱
- 将输入获取到字符串中,然后验证输入(下面尚未完成)
- 检查下注是否有效(=100,=300,=500,bet)如果您将变量重命名为英语,并且在程序执行过程中仅翻译了一次commentsCall srand(),这将非常有用。永远不要将其放入循环中。不可能猜出
int bet=(bet1,bet2,bet3)的意思
。我建议您再次回顾变量和赋值的基础知识。我建议您调试程序并逐步完成每个部分,以便更好地理解为什么每个部分都没有按照您认为的那样工作。目前,这有几个问题,而且有点过于宽泛。是的,我看到了同时,也改变了它,现在它不再狂暴,它循环了我想要循环的数量,但它不允许我重新下注,我可以按下某个按钮,它会自动下注,当我在某个随机事件上使用此代码时,我做得很好,显示程序输出和您的输入,以及您实际期望的结果PPEN,那么有人可以帮助你,感谢很多帮助,只是一个问题发生,循环去看你的终止条件在每一个循环。你可以考虑(如我所说)替换提示输入允许一个“退出”或“退出”条目。或退出时输入
#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;
int kast1, kast2, kast3, kast4;
int bet1 = 100;
int bet2 = 300;
int bet3 = 500;
int bet=0; //assignment didn't make sense
int insattning=0;
int konto=0;
//deposit
int get_insattning()
{
int good = 0;
while( !good )
{
cout<<"deposit"<<endl; //prompt for deposit
cin>>insattning;
if(insattning>5000 || insattning<0)//Makes sure so that my deposit is between 0-5000
{
cout<<"insattning fel, var vänlig och gör rätt denna gången"<<endl;
}
else good = 1;
}
cout<<"du har nu satt in" <<insattning<<"kr"<<endl;
return insattning;
}
//bet
int get_bet()
{
int good = 0;
int bet;
std::string validbets = "";
if(konto<100){ cout<<"you need more money"; return 0; }
while( !good )
{
cout<<"var vänlig och placera ditt bet"<<endl;
if(konto>=100){ validbets = "bet1=100"; }
if(konto>=300){ validbets += ", bet=300"; }
if(konto>=500){ validbets += ", bet=500"; }
cout<<validbets<<endl;
cin>>bet;
if( bet >= konto ) {
cout<<"you don't have enough money"<<endl;
continue;
}
if (bet==500){ //Makes sure so that i have the money to bet, and if i dont have the money, i can just put in more
cout<<"du har så här mycket på kontot nu "<<konto<<" kr"<<endl;
good = 1;
}
else if(bet==300){
cout<<"du har så mycket på kontot nu "<<konto<<" kr"<<endl;
good = 1;
}
else if(bet==100){
cout<<"du har nu bettat "<<bet<<" kr"<<endl;
good = 1;
}
else {
cout<<"you must place valid bet"<<endl;
continue;
}
}
return bet;
}
int main()
{
int count = 0;
int bet;
srand(time(0));
for( count=0; (count < 3); count++)
{
konto = get_insattning();
if (konto<100)
{
cout<<"du har inte nog med pengar, vänligen sätt in pengar"<<endl;
continue;
}
cout<<"och du har så här mycket i ditt konto "<<konto<<" kr"<<endl;
bet = get_bet();
//when you bet, reduce konto by bet
konto = (konto - bet);
{
cout<<"slå tärningar"<<endl;
kast1 = rand() % 6 + 1;
kast2 = rand() % 6 + 1;
kast3 = rand() % 6 + 1;
kast4 = rand() % 6 + 1;
cout<<"Du fick"<<kast1<<endl;
cout<<"du fick"<<kast2<<endl;
cout<<"datorn fick"<<kast3<<endl;
cout<<"datorn fick"<<kast4<<endl;
}
//did you win or lose?
//win? add money to konto
//lose? you have already deducted from konto
}
system ("pause");
}