Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的高或低游戏帮助_C - Fatal编程技术网

C中的高或低游戏帮助

C中的高或低游戏帮助,c,C,我在试着做一个高或低的游戏。掷三个骰子,加起来,然后猜高或低。当我到达高潮或低谷时,我遇到了一些问题。当我运行程序时,它在我回答高或低之前结束。此外,我不知道如何使用while循环继续,直到用户输入错误的猜测。这是我到目前为止所拥有的 #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> int main(void) { srand((uns

我在试着做一个高或低的游戏。掷三个骰子,加起来,然后猜高或低。当我到达高潮或低谷时,我遇到了一些问题。当我运行程序时,它在我回答高或低之前结束。此外,我不知道如何使用while循环继续,直到用户输入错误的猜测。这是我到目前为止所拥有的

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


int main(void)
{
    srand((unsigned)time(NULL));

    int dice1, dice2, dice3, dicetotal, i; // Variables
    char startGAMEendGame;

    // Initializing variables
    dice1 = 1 + rand() % 6;
    dice2 = 1 + rand() % 6;
    dice3 = 1 + rand() % 6;
    dicetotal = dice1 + dice2 + dice3;

    // Asking the user if they would like to start the game
    printf("\nWould you like to play a game? < Y / N > ");
    scanf("%c", &startGAMEendGame);

    // First If statement to start the game
    if ( startGAMEendGame == 'Y' || startGAMEendGame == 'y')
    {
        int dice1, dice2, dice3, dicetotal, i; // Variables
        char higherOrlower;

        i = dicetotal;                          // Initializing variables
        dice1 = 1 + rand() %6;
        dice2 = 1 + rand() %6;
        dice3 = 1 + rand() %6;
        dicetotal = dice1 + dice2 + dice3;

        printf("\nAwesome. Let's get started with a simple game of higher or lower.\nYou get to guess until you are wrong. \n");

        printf("\nYour first three rolls are %d, %d, %d, and their sum is %d. \n", dice1, dice2,              
        dice3, dicetotal);

        printf("\nWhat is next your guess? Higher or Lower? < H / L > ");
        scanf("%c", &higherOrlower);

        if ( dicetotal > i && higherOrlower == 'H' )
        {
            printf("\nCongratulations! You guessed correctly. ");
        }

        if (dicetotal < i && higherOrlower == 'L')
        {
            printf("\nCongratulations! You guessed correctly. ");
        }
    }

    if (startGAMEendGame == 'N' || startGAMEendGame == 'n')
    {
        printf("\nBummer. Maybe some other time. ");
    }
}
#包括
#包括
#包括
#包括
内部主(空)
{
srand((无符号)时间(NULL));
int dice1,dice2,dice3,dicetotal,i;//变量
charstartgameendgame;
//初始化变量
dice1=1+rand()%6;
骰子2=1+rand()%6;
dice3=1+rand()%6;
骰子总数=骰子1+骰子2+骰子3;
//询问用户是否愿意开始游戏
printf(“\N您想玩游戏吗?”);
scanf(“%c”和startGAMEendGame);
//第一个If语句开始游戏
如果(startGAMEendGame='Y'| startGAMEendGame='Y')
{
int dice1,dice2,dice3,dicetotal,i;//变量
焦高或低;
i=dicetotal;//初始化变量
dice1=1+rand()%6;
骰子2=1+rand()%6;
dice3=1+rand()%6;
骰子总数=骰子1+骰子2+骰子3;
printf(“\nAwesome。让我们开始一个简单的高或低的游戏。\n你可以猜测,直到你错为止。\n”);
printf(“\n您的前三卷是%d,%d,%d,它们的总和是%d.\n”,骰子1,骰子2,
骰子3,骰子总数);
printf(“\n您下一步的猜测是什么?高还是低?”;
扫描频率(“%c”和“高-低”);
如果(DICETOTALL>i&&higherOrlower=='H')
{
printf(“\n语法!您猜对了。”);
}
如果(DICETOTALL
在第二个
中的
%c
之前添加一个空格scanf
将解决此问题


这是因为在输入第一个字符后,
scanf
不会使用
\n
字符。由于enter键(
\n
)也是一个字符,它会被
scanf(“%c”、&higherOrlower)使用if
将永远不会执行,程序将结束。在%c之前的空格将丢弃所有空格和空格。

我已为您重新格式化了代码。我建议您调试并逐步浏览所有行,看看哪一行的行为与预期不同,然后在了解更多详细信息后再回来征求反馈。我才刚刚开始。谢谢你的建议!这么简单的修复!非常感谢,还有一个问题。因为我想在输入high或low后继续游戏,所以在第一个if语句之前我需要一段时间或for循环,对吗?@RonnieMatteini,只是high或low部分还是整个游戏?
while
loop更适合,但是请注意,循环永远不会结束,因为用户再也没有机会结束游戏了。我的教授还没有介绍do while循环。你能简单解释一下我将如何在这个游戏中使用它吗?