作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建一个具有以下规则的骰子游戏:
我已经能够做到这一点,但现在我必须通过调用函数来运行它。我被困在这一点上 - 我的代码在下面,但我不知道如何继续。
#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/
我是一名优秀的程序员,十分优秀!