gpt4 book ai didi

c - 如何在C程序中调用函数?

转载 作者:行者123 更新时间:2023-11-30 21:25:01 24 4
gpt4 key购买 nike

我正在尝试构建一个具有以下规则的骰子游戏:

  1. 您掷了三个骰子
  2. 如果您掷骰且每个骰子为 6,您将赢得 100 克朗
  3. 如果您掷骰且每个骰子都相同(但不是 6 个),您将赢得 50kr
  4. 如果您掷骰且两个骰子相同,您将赢得 10 kr
  5. 玩家每回合必须下注至少 10kr 才能玩
  6. 如果玩家获胜,系统会询问他们是否想再次玩
  7. 当玩家的资金低于 10kr 时,玩家会被告知无法再玩游戏。

我已经能够做到这一点,但现在我必须通过调用函数来运行它。我被困在这一点上 - 我的代码在下面,但我不知道如何继续。

#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<stdbool.h>

int rolling(int dice1, int dice2, int dice3, int moneyinput, char keep_rolling ); // prototype
keep_rolling = 'y';

int main()
{
int d1, d2, d3, money, r;
char keep_rolling = 'n';
money = 100;
r = rolling(d1, d2, d3, money,keep_rolling);
srand((int)time(NULL));
printf("welcome to the game! \n \n");

//gameroll
printf("1.it cost you 10kr to play \n");
printf("2.if all the dicies are sixes you win 100kr \n");
printf("3. if all the dicies are alike except number (6) you win 50kr \n");
printf("4. if you get at least two alike you 1 \n");
printf("5. otherwise you win nothing\n");

printf("you have %d kr, if you wnat to play press (n) \n\n", money);
fflush(stdin);
keep_rolling = ((getchar()));
d1 = (rand() % 6 + 1);
d2 = (rand() % 6 + 1);
d3 = (rand() % 6 + 1);


}

int rolling(int dice1, int dice2, int dice3, int moneyinput, char keep_rolling) // def my function
{
keep_rolling = 'y';
do {
dice1 = (rand() % 6 + 1);
dice2 = (rand() % 6 + 1);// from 1 to 6
dice3 = (rand() % 6 + 1);

if (moneyinput < 10)
{
printf("you do not have enough money \n");
break; // exit the program
}
moneyinput -= 10;
if (dice1 == 6 && dice2 == 6 & dice3 == 6)
{
printf("you have won 100\n ");
moneyinput += 90;
}
else if (dice1 == dice2 == dice3)
{
printf("you have won 50kr \n");
moneyinput += 40;
}
else if (dice1 == dice2 || dice1 == dice3 || dice2 == dice3)
{
printf("you have won 10kr \n");
moneyinput += 10;
}
else
{
printf("Sorry! you have lost. god luck next time \n");
}
} while (keep_rolling == 'y');
system("pause");
}

最佳答案

您需要将您的问题移至滚动功能中。您的主要功能并不独立于您的滚动功能运行。

此外,您的 if 语句中存在一些逻辑错误,这将使您的结果与您想要的不同。

if (dice1 == 6 && dice2 == 6 & dice3 == 6)

应该是

if (dice1 == 6 && dice2 == 6 && dice3 == 6)

还有

else if (dice1 == dice2 == dice3)

应该是

else if (dice1 == dice2 && dice2 == dice3)

除此之外,您还需要从滚动函数返回。

关于c - 如何在C程序中调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33550179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com