【问题标题】:How can i repeat a switch statement in C?如何在 C 中重复 switch 语句?
【发布时间】:2019-12-04 17:10:57
【问题描述】:
#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 的值。 另外,如果程序中还有其他错误,请让我注意。如果我使用了不清楚的英语,我深表歉意。

【问题讨论】:

  • 添加标志valid,您将在switch 语句中设置该标志。然后while(!valid)....
  • 旁注 - 考虑将一些逻辑转移到函数中。它将使代码更易于阅读/维护,并使您获得更好的成绩!

标签: c loops switch-statement repeat


【解决方案1】:

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

祝你好运!

【讨论】:

    【解决方案2】:

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

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

    然后在main中:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多