繁体   English   中英

由于这个 function,我的 do while 循环是否不起作用?

[英]Is my do while loop not working due to this function?

我正在制作一个程序,它要求用户猜测计算机正在考虑的数字 1-100。

在程序结束时,当用户猜对了数字时,我试图让程序询问用户是否想再次播放(重新启动程序)。

为了解决这个问题,我尝试使用do while loop & char repeat; . 循环几乎从程序的开始一直延伸到结束,尽管没有成功。 有谁知道我做错了什么? 是因为 function talfunktion ,循环不会通过吗?

代码:

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

int talfunktion (int tal, int guess, int tries, char repeat);

int main () {

do {
    srand(time(NULL));
    int tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of 
    int guess; //guess is the guessed value of the user
    int tries = 0; // amount of tries it took until getting correct
    char repeat;


    printf("Psst, the right number is: %d \n", tal); // remove later, not relevant to uppg.

    printf("Im thinking of a number between 1 and 100, guess which!");
    printf("\nEnter: ");
    scanf("%d", &guess);
    guess = talfunktion(tal, guess, tries, repeat);

    getchar();
    getchar();
    return 0;

    }

    int talfunktion(int tal, int guess, int tries, char repeat) {
        do {
            if (guess < tal) {
                tries++;
                printf("\nYour guess is too low, try again!");
                printf("\nEnter: ");
                scanf("%d", &guess);
            }
            else if (guess > tal) {
                tries++;
                printf("\nYour guess is too high, try again!");
                printf("\nEnter: ");
                scanf("%d", &guess);
            }
        } while (guess > tal || guess < tal);

        if (guess == tal) {
            printf("\nCongratulations, that is correct!");
            tries++;
            printf("\nYou made %d attempt(s)", tries);
            printf("\nPlay Again? (y/n)");
            scanf("%c", &repeat);
    }
} while (repeat == 'y' || repeat == 'Y');


}



在描述问题的评论中提到了几件事,你应该看的事情:

  • 不要在另一个 function 中定义 function
  • 小心放置返回语句的位置
  • 使用字符测试时,对变量使用char类型
  • 考虑简化您的逻辑比较。 (例如guess > tal || guess < talguess != tal相同)
  • 确保放置自动变量,以便它们在使用时可见。
  • 在格式说明符中放置空格: " %c"以便scanf()使用换行符。 (而不是过度使用getchar()

这是您的代码的简化版本,具有修改后的maintalfunktion功能...

char talfunktion(int tal);

int main (void) {
     int tal=0;//remove from inside {...} to make it visible to rest of function
     char repeat = 'n';

     srand(time(NULL));
     tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of 

    do {

            repeat = talfunktion(tal);

        }while((tolower(repeat) == 'y'));

        return 0;
}

char talfunktion(int tal)//do all relevant work in function and return 
{                        //only what is necessary
     int guess = 0;
     char repeat = 'n';


    printf("Im thinking of a number between 1 and 100, guess which!");
    printf("\nEnter a number from 1 to 100: ");
    scanf("%d", &guess);
    if((guess < 1) || (guess > 100))
    {
        printf("Entered out of bounds guess...\n");
    }
    else if (guess != tal)
    {
        if(guess < tal) printf("guess too small\n");
        else printf("guess too large\n");
        printf("Try  again? <'n' or 'y'>\n");
        scanf(" %c", &repeat);//space in format specifier to consume newline character
        if(tolower(repeat) != 'y') return 'n';//tolower() allows both upper and lower case
    }
    else
    {
        printf("Congratulations: You guessed right.\n");
        printf("Play again? <'n' or 'y'>\n");
        scanf(" %c", &repeat);
    }
    return repeat;
}

这是一种可能的解决方案

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

void talfunktion(int tal, int guess, int* tries)
{
            if (guess < tal)
            {
                (*tries)++;
                printf("\nYour guess is too low, try again!");
            }
            else if (guess > tal)
            {
                (*tries)++;
                printf("\nYour guess is too high, try again!");
            }
            else if (guess == tal)
            {
                (*tries)++;
                printf("\nCongratulations, that is correct!");
                printf("\nYou made %d attempt(s)", *tries);
            }
}

int main (void)
{
    int tal; //tal is the correct value that the code is thinking of
    int guess; //guess is the guessed value of the user
    int tries = 0; // amount of tries it took until getting correct
    char playAgain;

    do {
            srand(time(NULL));
            tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of
            printf("\nIm thinking of a number between 1 and 100, guess which!");
            printf("\nEnter: ");
            scanf("%d", &guess);
            talfunktion(tal, guess, &tries);

            printf("\nPsst, the right number is: %d", tal); // remove later, not relevant to uppg.
            getchar(); //to halt the code for taking the input

            if(guess == tal)
            {
                tries = 0;
                printf("\nPlay Again? (y/n)\n");
                scanf("%c", &playAgain);
            }

    } while (playAgain != 'n');

return 0;
}

暂无
暂无

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

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