C 程序 Switch 和 If 语句

标签 c if-statement switch-statement playing-cards

所以我正在尝试创建一个 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/

相关文章:

c++ - 哈希键和值

c - 遵循原作者的编码风格?即使它很可怕/懒惰/让你的眼睛流血?

c - gcc 警告 : incompatible pointer type

c - 使用二分搜索在排序数组的正确位置插入一个元素以找到正确的位置,但是出现运行时错误

c# - 是否可以检查 C# 中 switch case 语句中的空值

c - 在 TMS320C6713 DSK 中运行代码时出错?

mysql - SQL多重依赖UPDATE ON,什么最有效?

Java - 检查嵌套在 HashMap 中的 ArrayList 中的值是否存在

c - 为什么我的 switch 在 C 中会在接近整数限制时不稳定?

c++ - if条件树给switch语句效率带来麻烦