简体   繁体   English

是或否退出switch语句

[英]yes or no to exit a switch statement

Is it possible to create yes or no function to be called to exit a switch statement. 是否可以创建yes或no函数以退出switch语句。 if (y)es is hit it would exit. 如果(y)es被击中,它将退出。 If (n)o is hit it would loop back to the switch's options. 如果(n)o被击中,它将循环返回到开关的选项。 if so how would this be done. 如果是这样,将如何进行。 This is what I have so far. 到目前为止,这就是我所拥有的。

I hope this helps to clarify what I am trying to do 我希望这有助于阐明我要做什么

int yes_no(void) {

    scanf("%d", &option);

    while (option == 'y' || option == 'Y' || option == 'n' || option == 'N') {
        if (option == 'y' || option == 'Y') {
            return 1;
        } else
        if (option == 'n' || option == 'N') {
            return 0;
        } else {
            printf("Only (Y)es or (N)o are acceptable");
        }
    }
}

int main(void) {
    int select;

    do {
        printf("0 exit");
        printf("1 hello");

        scanf("%d", &select);

        switch (select) {
          case 0:
            printf("Exit the program? (Y)es or (N)o :");
            yes_no(); // if (n)o is hit it will loop back to the menu
            break;
          case 1:
            printf("hello how r u doing");
            break;
          default:
            printf("not accepted number");
        }
    } while (select != 0);
}

Consider the following yes_no function. 考虑以下yes_no函数。

int yes_no() {
  char option;

  while (1) {
    printf("(y/n): ");
    if (scanf(" %c", &option) != 1) {
      printf("Error occurred while reading option.\n");
      continue;
    }

    if (option == 'y' || option == 'Y') {
      return 1;
    } else if (option == 'n' || option == 'N') {
      return 0;
    } else {
      printf("Only (y)es or (n)o are acceptable.\n");
    }
  }
}

You have a return value of 1 if given a yes input, and 0 in case of no . 如果输入为yes ,则返回值为1如果为no ,则返回值为0 With this in mind, in the case 0 code block of the switch statement, the return value of this function can be captured, which can then be used to either break the loop or do nothing. 考虑到这一点,在switch语句的代码块为case 0case 0 ,可以捕获此函数的返回值,然后将其用于中断循环或不执行任何操作。 For example: 例如:

int main(void) {
  int select;
  int continue_loop = 1;

  printf("Press 0 to exit\n");
  printf("Press 1 for hello\n\n");

  while (continue_loop) {
    printf("(0/1): ");
    if (scanf(" %d", &select) != 1) {
      printf("Error occurred while reading number.\n");
      continue;
    }

    switch (select) {
      case 0:
        printf("Exit the program? (y)es or (n)o;\n");
        int make_sure = yes_no();  // yes_no() returns 1 if 'yes', and 0 if 'no'
        // If 'yes', break while loop by setting continue_loop to 0.
        // Do nothing in case of 'no'
        if (make_sure == 1) {
          continue_loop = 0;
        }
        break;
      case 1:
        printf("Hello, how are you doing?\n");
        break;
      default:
        printf("Number not accepted.\n");
    }
  }

  return 0;
}

This should do the job, if I get your question correctly: 如果我正确地回答了您的问题,这应该可以完成工作:

int yes_or_no()
{
  do
  {
    char option = 0;
    scanf(" %c", &option);
    if (option == 'y' || option == 'Y')
      return 1;
    if (option == 'n' || option == 'N')
      return 0;
    printf("Only (Y)es or (N)o are acceptable");
  }
  while( 1 );
}

with switch you can perform your code better: 使用switch可以更好地执行代码:

int yes_no(void) {
scanf("%d", &option);
while (1){
switch(option){
case 'y':
case 'Y':
return 1;
case 'n':
case 'N':
return 0;
default:
printf("invalid input.try again");
scanf("%d", &option);
}
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM