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

问题描述 投票:1回答:2
#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。另外,如果程序中还有其他错误,请注意。如果我使用不清楚的英语,我深表歉意。

c loops switch-statement repeat
2个回答
0
投票

解决方案是实际上添加一个变量/标志,以指示您是否实际上收到了正确的语句。如果收到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之间变化,具体取决于您要表示的内容。

祝你好运!


0
投票

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

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 ) ;
  }

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

© www.soinside.com 2019 - 2024. All rights reserved.