某些陈述打印两次

时间:2014-10-03 05:51:32

标签: c

在我写的刽子手计划中,有两个相同字母的单词,例如" eel"或者"蜜蜂,"当用户输入" e"第一次,然后试图进入" e"对于第二个提示,将显示"您已经猜到了这封信"两次。我该如何解决这个问题?

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


int main(){

char w[13][3] = {
        { 'c', 'a', 't' }, //0
        { 'd', 'o', 'g' }, //1
        { 'r', 'a', 't' }, //2
        { 'e', 'e', 'l' }, //3
        { 'c', 'o', 'w' }, //4
        { 'o', 'w', 'l' }, //5
        { 'e', 'm', 'u' }, //6
        { 'b', 'a', 't' }, //7
        { 'e', 'l', 'k' }, //8
        { 'p', 'i', 'g' }, //9
        { 'b', 'e', 'e' }, //10
        { 'h', 'e', 'n' }, //11
        { 'f', 'o', 'x' }, //12
};

char u,
    newline,
    dis[16];

int random,
    guesses = 3,
    finish = 0;

_Bool successfulGuess = false;

srand(time(NULL));
random = rand() % 13;

    printf("Animal %d\n", random); //check random number
    printf("---------\n\n");
    printf("Enter a letter: ");
    u = getchar();
    newline = getchar();

    for (int i = 0; i < 3; i++){

        if (w[random][i] == u){
            successfulGuess = true;
            dis[i] = u;
        }
        else {

            dis[i] = '_';
        }
    }

    for (int j = 0; j < 3; j++){
        dis[j] = dis[j];

    }

    printf("\n");

    for (int i = 0; i < 3; i++){
        printf("%c", dis[i]);
    }

    if (successfulGuess == false){
        --guesses;
    }
    printf("\n\nGuesses left: %d", guesses);
    printf("\n\n");

while (guesses > 0){
    finish = 0;
    successfulGuess = false;
    printf("Enter a letter: ");
    u = getchar();
    newline = getchar();

    for (int i = 0; i < 3; i++){

        if (u == dis[i]){
            successfulGuess = true;
            printf("\nYou already guessed this letter.\n");
            printf("\ninput = dis[i]\nGuesses left: %d\n\n", guesses);
        }
        else if (w[random][i] == u){
            successfulGuess = true;
            dis[i] = u;
            printf("\ninput = array char\nGuesses left: %d\n\n", guesses);
        }
    }
        for (int i = 0; i < 3; i++){
            printf("%c", dis[i]);
        }

        if (successfulGuess == false){
            guesses--;
            printf("\n\nbool statement\nGuesses left: %d\n\n", guesses);
        }

        if (guesses == 0){
            printf("Sorry, you've run out of guesses.");
        }

        for (int i = 0; i < 3; i++) {
            if (dis[i] != '_') {
                finish++;
            }
            if (finish == 3){
                printf("\n\nYou guessed the word!");
                guesses = 0;
            }
            else{
                continue;
            }
        }
        printf("\n\n");
}

system("pause");
}

1 个答案:

答案 0 :(得分:3)

即使您已经显示该消息,您也会循环三次。我认为应该打破这个信息:

for (int i = 0; i < 3; i++){

    if (u == dis[i]){
        successfulGuess = true;
        printf("\nYou already guessed this letter.\n");
        printf("\ninput = dis[i]\nGuesses left: %d\n\n", guesses);

        /* BREAK HERE */
        break;
    }