所以我正在尝试创建一个 C 代码程序,该程序将给出扑克牌的简写符号,并确定格式化的扑克牌。例子 输入:H 8 输出:红心8
Input: C 14
Output: Ace of Clubs
排名:2-10、11 J、12 Q、13 K、14 A 花色:梅花 C、方 block D、红心 H、黑桃 S 但在实现的最后阶段,我遇到了一些严重的问题。当我输入 D 5 时,程序运行良好,但输入 D 12 会使其列出皇后,然后是 jack ,然后是 12 颗钻石。
代码如下:http://pastebin.com/Tj4m6E2L 这是当前的 EXE:http://www.mediafire.com/download/4fy4syga2aj8n2j
感谢您提供的任何帮助。我是 C 代码新手,所以为了我的利益,请保持简单和愚蠢。
最佳答案
您在 switch
中缺少关键的 break
语句,例如
switch(rank)
{
case 14:
{
if(suite == 'H')
printf("Your card is the Ace of Hearts!");
else if(suite == 'C')
printf("Your card is the Ace of Clubs!");
else if(suite == 'D')
printf("Your card is the Ace of Diamonds!");
else
printf("Your card is the Ace of Spades!");
}
// <<< NB: case 14 "falls through" to case 13 here !!!
case 13:
...
将其更改为:
switch(rank)
{
case 14:
{
if(suite == 'H')
printf("Your card is the Ace of Hearts!");
else if(suite == 'C')
printf("Your card is the Ace of Clubs!");
else if(suite == 'D')
printf("Your card is the Ace of Diamonds!");
else
printf("Your card is the Ace of Spades!");
}
break; // <<< FIX
case 13:
...
对所有其他缺失的 break
语句重复此操作。
关于C 程序 Switch 和 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26344221/