二十一点计分程序与错误
BlackJack Scoring Program with Errors
我的代码有问题!当我以{"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";
}
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- xmake总是报告:错误:无法获取cxx的程序,为什么
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 分段错误当我试图运行程序时出错
- 显示错误输出的简单数组排序程序
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 在气泡排序程序中未声明错误功能
- 我在c ++矩阵计算器程序中找不到错误
- 叮当格式化程序多行格式配置错误
- 全局向量导致 C++ 程序结束时出现段错误
- 运行时间错误:程序跳过提示,以获取第二名和第三个名称
- Glibc 2.7 配置错误程序太旧
- 运行时错误:程序已停止响应
- OpenCV for Android:在 PATH 中找不到错误程序"sh"
- C++中的 scanf 错误 - 程序没有响应
- c++语法错误:程序以非零状态退出
- 运行时错误C++程序:检测到glibc,munmap_chunk(),指针无效
- libxml2错误程序c++