简体   繁体   English

如何正确设置开关状态循环?

[英]How do I make my switch statment loop properly?

When the characters AB or C are entered it returns their numerical value. 输入字符AB或C后,它将返回其数值。

The Default statement prints if anything is entered for the first time. 如果是第一次输入任何内容,则会显示Default语句。 THEN if you enter AB or C it prints properly but after that the Default statement prints no matter what. 然后,如果输入AB或C,它将正确打印,但是之后无论如何都将打印Default语句。

/*#define_USE_C99_MATH*/
/*#include <stbool.h>*/
#include <stdio.h>

main ()
{
    int ABC;     /* I/O */
    char c1 = 'A'; /*Characters to be entered*/
    char c2 = 'B';
    char c3 = 'C';

    /*typedef enum { false, true } bool;*/
    printf("Please enter either A, B, or C (case sensitive):");
    ABC = getchar();
    /*bool loop = true;*/

    if ((ABC = getchar()) == c1 | c2 | c3)
    {
        do
        {
            switch (ABC)
            {
            case 'A':    
                printf("The numerical value of A is: %d\n", c1);
                break;

            case 'B':    
                printf("The numerical value of B is: %d\n", c2);
                break;  

            case 'C':    
                printf("The numerical value of C is: %d\n", c3);
                break;  

            default:
                printf("The character you have entered is not valid.\n");
                break;
            }
        } while ((ABC = getchar()) != c1 | c2 | c3);
    }
    else
    {
    }

    return 0;
}

Your error seems to be that you have entered an if statement which will work only if you enter 'A' 'B' or 'C'. 您的错误似乎是您输入了if语句,该语句仅在您输入“ A”,“ B”或“ C”时才有效。 In my opinion though, i think it would be better to not use if statements in this program. 不过,我认为,最好不要在该程序中使用if语句。 Rather, just keep the do while loop and the code will run fine. 相反,只要保持do while循环,代码就可以正常运行。

You should be using logical-OR || 您应该使用逻辑或|| instead of the bitwise-OR | 而不是按位或| ( you can still use | in this case but it helps the code be more readable ). 在这种情况下,您仍然可以使用| ,但它有助于使代码更具可读性 )。 And you can encounter a problem with using input functions because when the user enters an input via getchar() it actually sends 2 characters, say A and \\n so you need to consume the newline again via getchar() 您可能会遇到使用输入函数的问题,因为当用户通过getchar()输入输入时,它实际上会发送2个字符,例如A\\n因此您需要再次通过getchar()使用换行符

I've modified your code here, but it still does the same behavior: 我在这里修改了您的代码,但是它仍然具有相同的行为:

#include <stdio.h>

main ()
{
    char ABC;
    char c1 = 'A';
    char c2 = 'B';
    char c3 = 'C';

    printf("Please enter either A, B, or C (case sensitive):");
    ABC = getchar();

    while(ABC != c1 && ABC != c2 && ABC != c3)
    {
        getchar(); // Consume \n
        printf("The character you have entered is not valid.\n");
        ABC = getchar();
    }

    switch (ABC)
    {
    case 'A':    
        printf("The numerical value of A is: %d\n", c1);
        break;

    case 'B':    
        printf("The numerical value of B is: %d\n", c2);
        break;  

    case 'C':    
        printf("The numerical value of C is: %d\n", c3);
        break;  
    }

    return 0;
}

Why not just remove the if statement, because if you do that you will have a chance to have your default case be true, another reason why you may be having errors is due to this: 为什么不只删除if语句,因为如果这样做,您将有机会使默认大小写为true,则可能由于以下原因而导致出现错误的另一个原因是:

while ((ABC = getchar()) != c1 | c2 | c3);

make it into: 使其成为:

while ((ABC = getchar()) != c1 | ABC != c2 | ABC != c3);

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

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