我的do while循环是不是因为这个功能而不能工作?

我做了一个程序,它要求用户猜一个计算机正在思考的1-100的数字,在程序结束时,当用户猜到正确的数字时,我试图让程序问用户是否想再玩一次(重新启动程序)。

在程序的最后,当用户猜对了数字,我试图让程序问用户是否想再玩一次(重新启动程序)。

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

代码:我做了一个程序,要求用户猜测计算机正在思考的1-100的数字。

#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');


}



解决方案:

这是一个可能的解决方案

#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;
}

给TA打赏
共{{data.count}}人
人已打赏
未分类

如何在Mule 3 dataweave中映射特殊字符字段?

2022-9-11 19:48:38

未分类

我想在运行捕捉块时打印无效的输入,但它没有发生。

2022-9-11 19:48:40

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索