简体   繁体   English

在 C 中用 for 循环循环开关

[英]Looping a switch with for loop in C

I can't get my for loop switch to work.我无法让我的 for 循环开关工作。 I want the program to repeat the question if the user type a letter other than 'y','Y','n','N'.如果用户键入除“y”、“Y”、“n”、“N”以外的字母,我希望程序重复该问题。 Can someone help me fix please?有人可以帮我解决吗?

#include <stdio.h>

int main(void) {

  int flag = 0;
  char mstatus;

  printf("Are you married?\n");
  scanf(" %c", &mstatus);

  for (; flag == 1;) {
    printf("Are you married?\n");
    scanf(" %c", &mstatus);
    switch (mstatus) {
    case 'y':
    case 'Y':
      printf("You have answer yes for married");
      flag = 1;
      break;
    case 'n':
    case 'N':
      printf("You have answer no for married");
      flag = 1;
      break;
    default:
      printf("please re-enter a valid answer");
      scanf(" %c", &mstatus);
    }
  }
  return 0;
}

you have several problems in your code你的代码有几个问题

The first couple第一对

printf("Are you married?\\n"); scanf(" %c", &mstatus);

is done for nothing because you do not use the read answer, remove these lines一事无成,因为您不使用阅读答案,请删除这些行

In

for (; flag == 1;) {

you immediately exit the loop because you initialized flag to 0, and this is not consistent with how you change the value of flag , compare it with 0 rather than 1您立即退出循环,因为您将flag初始化为 0,这与您更改flag值的方式不一致,将其与 0 而不是 1 进行比较

In

default: printf("please re-enter a valid answer"); scanf(" %c", &mstatus);

the scanf must be removed because you do not use read answer必须删除scanf因为您不使用阅读答案

Out of that, because you want to do the loop at least one time to ask for the answer and manage it, it is very more readable to use a do ...while除此之外,因为您希望至少循环一次以询问答案并对其进行管理,所以使用do ...while更具可读性

Also add a final newline when you print something打印内容时还要添加最后的换行符

Example :例子 :

#include <stdio.h>

int main(void) {

  int flag = 0;

  do {
    char mstatus;

    printf("Are you married?\n");
    scanf(" %c", &mstatus);

    switch (mstatus) {
    case 'y':
    case 'Y':
      printf("You have answer yes for married\n");
      flag = 1;
      break;
    case 'n':
    case 'N':
      printf("You have answer no for married\n");
      flag = 1;
      break;
    default:
      printf("please re-enter a valid answer\n");
    }
  } while (flag == 0);

  return 0;
}

Compilation and executions :编译和执行:

pi@raspberrypi:/tmp $ gcc -Wall d.c
pi@raspberrypi:/tmp $ ./a.out
Are you married?
a
please re-enter a valid answer
Are you married?
y
You have answer yes for married
pi@raspberrypi:/tmp $ ./a.out
Are you married?
d
please re-enter a valid answer
Are you married?
N
You have answer no for married
pi@raspberrypi:/tmp $ 

Of course in your case there is nothing after the loop, so you can also simplify all removing flag and its management and replace the two first break by a return 0 , of course in that case the loop can be for(;;) or while(1) etc当然,在您的情况下,循环之后没有任何内容,因此您还可以简化所有删除标志及其管理,并将两个第一个中断替换为return 0 ,当然在这种情况下,循环可以是for(;;)while (1)

Probably please enter a valid answer is a better choice than please re-enter a valid answer because by definition the user never enter a valid answer so he cannot re-enter one可能请输入有效答案请重新输入有效答案更好,因为根据定义,用户永远不会输入有效答案,因此他无法重新输入

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

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