C if语句被跳过

时间:2014-02-03 20:09:11

标签: c if-statement random

我目前正在学习C,并且正在尝试与某些课程所要求的相反。 miniMasterMind是我发现的一项任务,用户猜测计算机随机生成的数字。我试图对其进行简单的翻转,用户告诉计算机其猜测是否对用户生成的3位数字是正确的。

我认为我是一个完全正常工作的程序,除了我的3 if语句要求用户输入有时不起作用。我看不出任何理由,但在编译之后我经常会发现一两个if语句只是跳过用户输入。我在每一步之后加入系统(“暂停”)以便于查看。

游戏中的每一个回合,一组不同的if语句似乎都会破裂。为什么会这样?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    // Initialize variables
    int UCMain = 0;
    int CG1 = -1, CG2 = -1, CG3 = -1;
    int win1 = -2, win2 = -3, win3 = -4;
    char check1 = 'A', check2 = 'B', check3 = 'C';
    int turnCount = 0;


    // Print out start screen
    printf("Welcome to masterMind reversed!  Let's see how this works out!\n\n");

    // Accept user input
    printf("Type in a three digit number for the computer to guess.\n");
    scanf_s("%d", &UCMain, 3);

    const int UC1 = (UCMain / 100) % 10;
    const int UC2 = (UCMain / 10) % 10;
    const int UC3 = UCMain % 10;

    printf("\nTest print, UC1: %d UC2: %d UC3: %d\n", UC1, UC2, UC3);
    system("Pause");

    // Start game loop
    while (turnCount < 10)
    {

        // Random number gen
        srand((int)time(0));

        // 1st number
        if (win1 == UC1)
        {
            CG1 = win1;
        }
        else if (win1 != UC1)
        {
            CG1 = rand() % 10;
        }

        // 2nd number
        if (win2 == UC2)
        {
            CG2 = win2;
        }
        else if (win2 != UC2)
        {
            CG2 = rand() % 10;
            if (CG2 == CG1)
            {
                CG2 = rand() % 10;
            } // End unique check
        }

        //3rd number
        if (win3 == UC3)
        {
            CG3 = win3;
        }
        else if (win3 != UC3)
        {
            CG3 = rand() % 10;
            if (CG3 == CG2 || CG3 == CG1)
            {
                CG3 = rand() % 10;
            } // End unique check
        }
        // End random number generation

        printf("The computer guesses: %d%d%d\n", CG1, CG2, CG3);
        system("Pause");

        // Check if numbers are correct
        if (win1 != UC1)
        {
            printf("Is the first number correct? Y/N\n");
            scanf_s("%c", &check1, 1);
            if (check1 == 'Y')
            {
                win1 = UC1;
            } // 
        }// End 1st check

        system("pause");

        if (win2 != UC2)
        {
            printf("Is the second number correct? Y/N\n");
            scanf_s("%c", &check2, 1);
            if (check2 == 'Y')
            {
                win2 = UC2;
            } // 
        }// End second check

        system("pause");

        if (win3 != UC3)
        {
            printf("Is the third number correct? Y/N\n");
            scanf_s("%c", &check3, 1);
            if (check3 == 'Y')
            {
                win3 = UC3;
            } // 
        }// End third check

        system("pause");

        // Check if game is over
        if (win1 == UC1 && win2 == UC2 && win3 == UC3)
        {
            printf("The computer wins!");
        }

        turnCount++;

    } // End while

    // Win/lose state
    if (turnCount == 10)
    {
        printf("The computer loses!");
    }
}

2 个答案:

答案 0 :(得分:1)

它没有被真正跳过: 它是一个字符的换行符:'\ n'

使用带有空格的scanf:

  scanf(" %c", &b); // this one will work instead

告诉scanf应该忽略stdin上留下的任何空白字符(包括换行符'\ n')。

请阅读有关scanf here

的更多信息

答案 1 :(得分:0)

scanf()正在阅读所有用户输入,包括换行符和回车符。如果用户键入“1”并按Enter键,您实际上将得到2(或3,取决于平台)输入字符,一个用于数字,一个或两个用于换行符。当你只打算循环时,这将刺激你的循环的2或3次迭代。