我如何在 C 中重复 switch 语句?

How can i repeat a switch statement in C?

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

int main(void){
 int a;
 int attempt;
 int answer;
 int f;
 int difficulty;
 int end;

 printf("GUESS THE NUMBER\n");
 printf("guess the number chosen on a scale of 1 to 100\n");
 printf("\n");

do{
 srand(time(NULL));
 a=rand()%100+1;
 attempt=1;

 printf("Choose Difficulty\n");
 printf("1.Easy   2.Normal   3.Hard\n");
 scanf("%d", &difficulty);

 switch(difficulty){
   case 1:{
        f=15;
        break;
   }
   case 2:{
        f=10;
        break;
   }
   case 3:{
        f=5;
        break;
   }
   default: printf("Invalid value\n");
 }

 printf("\n");
 printf("You have %d attempts to guess the chosen number\n",f)
 printf("At each attempt you will be given a clue\n")

while(attempt<=f){
 int remaining=f-attempt;
 printf("\nattempt number %d:\n",attempt);
 scanf("%d",&answer);

 if(answer>a){
    if((difficulty=1)){
       if(answer-a<=5){
           printf("The value entered is slightly bigger\n");
           attempt++;
       }
       else if(answer-a>5&&answer-a<=10{
           printf("The value entered is bigger\n");
           attempt++;
       }
       else if(answer-a>10){
           printf("The value entered is much bigger\n");
           attempt++;
       }
    }
 }
 else if((answer<a){
    if((difficulty=1)){
       if(a-answer<=5){
           printf("The value entered is slightly smaller\n");
           attempt++;
       }
       else if(a-answer>5&&answer-a<=10{
           printf("The value entered is smaller\n");
           attempt++;
       }
       else if(a-answer>10){
           printf("The value entered is much smaller\n");
           attempt++;
       }
    }
 }
 else if((answer=a)){
    printf("GUESSED\n");
    printf("attempts made: %d\n",attempt);
    printf("remaining attempts: %d\n,remaining);
    printf("Do you want to play again? 1=Yes  0=No\n");
    scanf("%d",&end);
    break;
 }

 }
 printf("\nGame Over\n");
 printf("the number chosen was %d\n",a);
 printf("Do you want to try again? 1=Yes  0=No\n");
 scanf("%d",&end);
}
while((end==1));
return 0;
}

这是我目前的程序,我想在开关中创建一个循环,以便在每次输入不正确的值时重复。我能怎么做? 不正确的值是指输入的任何不是 1,2 或 3 的值。 另外,如果程序中还有其他错误,请告诉我。如果我使用了不清楚的英语,我深表歉意。

解决方案是实际添加一个 variable/flag 来指示您是否确实收到了正确的声明。如果您收到 1 2 or 3,只需将该标志变为 1,并在 while 语句中简单地输入:while(!flag)。 所以代码将是:

int flag=1;
while(flag){
switch(difficulty){
   case 1:{
        f=15;
        flag=0;
        break;
   }
   case 2:{
        f=10;
        flag=0;
        break;
   }
   case 3:{
        f=5;
        flag=0;
        break;
   }
   default: printf("Invalid value\n");
 }
}

记住始终将标志设置为 1。 PS: 可以使用 int 变量设置标志,该变量在 0 和 1 之间变化,具体取决于您要表示的内容。

祝你好运!

另一种方法是编写一个方法来验证输入:

int validateInput( int difficulty ) {
    switch(difficulty){
    case 1:{
        return 15;
    case 2:
        return 10;
    '''
    default: 
        // write error message here
        return -1;
    }
}

然后在主要部分:

   int f = -1;
   ...
   while ( f < 0 ) {
      // read "difficulty" here
      ...
      f = validateInput( difficulty ) ;
  }

还包括一个特殊的难度值以允许用户放弃(避免无限循环)。