二十一点计分程序与错误

BlackJack Scoring Program with Errors

本文关键字:错误 程序 十一点 二十一      更新时间:2023-10-16

我的代码有问题!当我以{"T,Q,J,K,A"}的组合输入5张纸牌时,它不会计算分数,而是跳转到"你想再玩一次吗?"我相信是逻辑错误,但似乎找不到!有人能帮我解决这个挑战吗?

#include <iostream>
using namespace std;
int main()
{
    int cardnum, total=0, aceCount=0, i; 
    char face, ans;
    do
    {
    total = 0;
    cout << "How many Cards Do you have in your hands? (Between 2 and 5):n";
    cin >> cardnum;
    if(cardnum <2 || cardnum > 5)
       {
          cout << "Not a Valid number of cards!n";
       }
    cout << "Please enter Your Card Values.(2-9 or T, J,Q,K, A): n";
    for(i=0; i<cardnum; i++)
       {
          cin >> face;
          switch (face)
          {
             case '2' :
                  total+=2;
                  break;
             case '3' :
                  total+=3;
                  break;
             case '4' :
                  total+=4;
                  break;
             case '5' :
                  total+=5;
                  break;
             case '6' :
                  total+=6;
                  break;  
             case '7' :
                  total+=7;
                  break;
             case '8' :
                  total+=8;
                  break;
             case '9' :
                  total+=9;
                  break;
             case 't' :
             case 'j' :
             case 'q' :
             case 'k' :
             case 'T' :
             case 'J' :
             case 'Q' :
             case 'K' :
                  total+=10;
                  break;
             case 'A' :
             case 'a' :
                  total+=11;
                  aceCount++;
          }
      }
      if(total <=21)
      {
         cout << "Your Total Score is: " << total<<endl;
      }
      else if(aceCount > 0 && total > 21)
      {
         do
         {
            total-=10;
            aceCount--;
         }while (aceCount > 0 && total >21);
         if(total <=21)
        {
            cout << "Your Total Score is: " << total<<endl;
        }
      }
         else if(total > 21)
        {
            cout << "Your Total Score is: " << total<< " Which Means You Busted!n";
        }
    cout << "Do You Wish to Calculate Your Score Again? (Type y OR Y).n";
    cin >> ans;
    }while(ans == 'y' || ans=='Y');    
    cout << "GOOD BYE! Play NEXT TIME!n";
    system("PAUSE");
    return 0;
}   
if(total <=21)
{
    cout << "Your Total Score is: " << total<<endl;
}

你在两行都有这个,所以你想要的其他条件没有得到满足。只需将它的第二次迭代更改为:

if(total > 21)
{
    cout << "Your Total Score is: " << total<<endl;
}

修复缩进,这会使问题更明显。

当你在第一个else块中输入ace处理代码时,第三个else块将不会被执行。所以分数只有在<= 21时才会被打印出来。你应该在处理完a之后再做打印工作,这两个块应该是相互独立的。

未经测试,但代码应该看起来像:

  if(aceCount > 0 && total > 21)
  {
     do
     {
        total-=10;
        aceCount--;
     }while (aceCount > 0 && total >21);
  }
  if(total <=21)
        cout << "Your Total Score is: " << total<<endl;
  }
  else if(total > 21)
  {
        cout << "Your Total Score is: " << total<< " Which Means You Busted!n";
  }