如何让switch()在C中循环多次?

时间:2014-11-12 17:26:15

标签: c

我创造了这个水果机游戏。但是我想在打印最终输出然后得分之前多次循环输出。模拟真实老虎机的移动性质。当我尝试循环我的switch()语句时,不会产生任何输出。我该怎么做呢?

#include <stdio.h>
#include <unistd.h>


int main ()
{
  int firstReel, secondReel, thirdReel, loop;

  // Generating three random numbers
  srand(time(NULL));
  int rndOne = rand () %4;
  int rndTwo = rand () %4;
  int rndThree = rand () %4;

  // Assigning random numbers to clearer var names
  firstReel = rndOne;
  secondReel = rndTwo;
  thirdReel = rndThree;

  // Switch statements for each reel
  switch(firstReel){
    case 0:
      printf("Bell ");
      break;
    case 1:
      printf("Cherry ");
      break;
    case 2:
      printf("Orange ");
      break;
    case 3:
      printf("Horseshoe ");
      break;
  }

  switch(secondReel){
    case 0:
      printf("Bell ");
      break;
    case 1:
      printf("Cherry ");
      break;
    case 2:      
      printf("Orange ");
      break;
    case 3:
      printf("Horseshoe ");
      break;
  }

  switch(thirdReel){
    case 0:
      printf("Bell\n");
      break;
    case 1:
      printf("Cherry\n");
      break;
    case 2:
      printf("Orange\n");
      break;
    case 3:
      printf("Horseshoe\n");
      break;
  }
  // Win/lose conditions
  if (firstReel == secondReel || firstReel == thirdReel || secondReel == thirdReel)
    printf("Congratulations! You win!\n");
  else
  {
  printf("Sorry, you lose. Play again? (Y/N)\n");
  }
}

3 个答案:

答案 0 :(得分:0)

尝试使用如下所示的循环: 在这里,我运行循环了x次。你可以循环运行你想要的任何次数。

 int main ()
    {
      int firstReel;
      int i=0;
      // Generating three random numbers
      srand(time(NULL));

      // Assigning random numbers to clearer var names


    while(i<7)
    {
        firstReel = rand () %4;
      // Switch statements for each reel
      switch(firstReel){
        case 0:
          printf("Bell ");
          break;
        case 1:
          printf("Cherry ");
          break;
        case 2:
          printf("Orange ");
          break;
        case 3:
          printf("Horseshoe ");
          break;
      }
      i++;

    }
    }

答案 1 :(得分:0)

使用某种计数器/循环语句

int i=0;
while(i< 10){

       //Your switch statements
 i++;

 }

作为一个更好的编程实践,当开关输入不满足任何情况时,也要包括默认情况/场景..有助于保持代码结构化并避免任何混淆,同时显示其他值已被处理。例如:

 default: 
         printf("Invalid value entered");
         break;

答案 2 :(得分:0)

这将显示卷轴旋转并减慢到最终模式(在控制台中)。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SYMBOLS     4
#define REELS       3
#define SPINS       20

char *symbol [SYMBOLS] = {"   Bell   ", "  Cherry  ", "  Orange  ", "Horseshoe "};
int reel [REELS];

int main(int argc, char **argv)
{
    int s, r, elap, tix, counts;

    srand ((unsigned)time(NULL));
    for (s=SPINS; s>0; s--) {
        printf ("\r");
        for (r=0; r<REELS; r++) {
            reel [r] = rand() % SYMBOLS;
            printf ("%s", symbol [reel [r]]);
        }
        tix = clock();
        counts = CLOCKS_PER_SEC / s;
        do {
            elap = clock() - tix;
        }
        while (elap < counts);
    }

    printf ("\n");
    for (r=1; r<REELS; r++)
        if (reel [r] != reel [r-1])
            break;
    if (r < REELS)
        printf ("You lost!\n");
    else
        printf ("You won!\n");
    return 0;
}