【问题标题】:Basic C++ Dice game基本 C++ 骰子游戏
【发布时间】:2013-10-14 00:41:10
【问题描述】:

我在这里遇到问题,如果有人可以在这里帮助我,那就太好了。这是我第一次使用这个程序,所以不要评判。

#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");
}

所以这里的问题是,出于某种原因,我总是下注 500,即使输入 bet1 或 bet2,我也不知道如何解决这个问题。然后我的循环函数(int count 0; while(count &lt; 3)count++) 它开始无限循环,无需我按下任何东西,即使我在简单的编码中使用相同的循环函数,比如输入一些 cout

【问题讨论】:

  • 如果您将变量重命名为英文并翻译 cmets 将会很有帮助
  • 在程序执行过程中只调用 srand() 1 次。永远不要把它放在一个循环中。
  • 不可能猜出int bet=(bet1, bet2, bet3) 的意思。我建议您再次查看有关变量和赋值的基础知识。
  • 我建议您调试程序并逐步检查每个部分,以便您更好地理解为什么每个部分没有按照您的想法执行。就目前而言,这有几个问题,对于 SO 来说有点过于宽泛。

标签: c++ c++11 dice


【解决方案1】:
int bet1 = 100;
int bet2 = 300;
int bet3 = 500;
int bet=(bet1, bet2, bet3)

最后一行的计算结果如下:100、300、500。逗号分隔的表达式列表的结果将是最后一个值,即 500。因此您的投注变量将始终设置为 500。

【讨论】:

    【解决方案2】:

    您在代码下方的评论中声明的内容 (int count 0; while(count &lt; 3)count++) 看起来像是 forwhile 循环的某种奇怪混合。请再次查看您的 C++ 教科书/在线教程,了解如何编写正确的循环。

    在您显示的代码中,在您的 while 循环中,您不会修改 count 变量 - 因此如果循环前 count count++ 语句实际上在您的主 while 循环之外!

    当你想做某事固定次数时,推荐使用for循环,这样更难忘记增量!

    【讨论】:

    • 是的,我也看到了,所以改变了它,它现在开始横冲直撞了,它循环了我想要循环的数量,但它不允许我重做我的赌注,我可以只需按下某个东西,它就会自动执行,我打赌当我随机使用此代码时,我会正常工作
    • 向我们展示程序输出以及您的输入,以及您实际期望发生的情况,然后有人可以帮助您
    【解决方案3】:

    你在循环之外增加count ,所以它总是为零。要么将其移动到循环内(正确的缩进是关键!),要么使用for 循环代替:

    for (count = 0; count < 3; ++count) { ... }
    

    【讨论】:

      【解决方案4】:

      一些建议,

      • 将您的存款提示(插入)放入函数中
      • 将您的投注提示放入函数中
      • 在提示下注之前检查是否有足够的钱
      • 获取输入到字符串中,然后验证输入(下面尚未完成)
      • 检查投注是否有效(=100,=300,=500, 投注

      这里有这些方便的功能,

      #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;
      }
      

      我不清楚您是要 100,300、500 或 3 个赌注中的 1 个赌注。这是第一个,

      //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;
              }
      

      你需要编写代码来判断你是赢还是输,然后在赢的时候添加到konto,

              //did you win or lose?
              //win?  add money to konto
              //lose?  you have already deducted from konto
          }
          system ("pause");
      }
      

      这些建议应该可以帮助您修复程序。

      【讨论】:

      • 非常感谢您的帮助,只是出现了一个问题,循环无休止
      • 查看每个循环的终止条件。您可能会考虑(如我所说)将提示输入替换为允许“退出”或“退出”条目。还是输入
      猜你喜欢
      • 2012-11-27
      • 2012-02-29
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      相关资源
      最近更新 更多