我需要弄清楚如何解决这个问题。 [英] I need to figure out how to fix this problem.

查看:76
本文介绍了我需要弄清楚如何解决这个问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩一个二十一点游戏,我仍然是一个更大的人,我在编写功能方面遇到了一些问题。

i希望有这样的东西...



例如:

欢迎来到CS介绍二十一点! 
首先输入一个随机种子:
5
你现在有1000美元。你想赌多少钱?
1300
我们的赌注必须至少为1且最多为1000,请再试一次:
0
您的赌注必须至少为1且最多为1000,请再试一次:
1000





我的尝试:



  int  main()
{
int money = 0 ;
int seed = 0 ;
int bet = 0 ;
printf( 欢迎来到CS介绍二十一点!\ n);
printf( 首先输入随机种子:\ n);
scanf( %d,& seed);
bet = bett(money);

return 0 ;
}
int bett( int x)
{
int y = 0 ;
printf( 你现在有%d假美元。你想赌多少钱?中,x);
scanf( %d,& y);
while (y> x)
{
printf( 您的赌注必须至少为1且最多为%d,请再试一次:,x);
scanf( %d,& y);
}
return (y);
}

解决方案

首先你必须弄清楚程序的逻辑。

我猜这是学校项目中的第一个作业,所以不太复杂。



首先你有两个输入限制:输入的赌注必须大于1且小于可用的资金。

硬编码值总是一个坏主意,所以首先声明一个常数为下限:

例如

< pre lang =c ++> #define MIN_BET 1





如果你看看函数bett(int x),你的逻辑不正确。

你用变量钱调用函数,但你已经把钱初始化为零。

你需要首先为货币分配一个初始值,这也应该是一个常数。

  #define INITIAL_MONEY 1000  





然后分配

 money = INITIAL_MONEY; 
bet = bett(money);

bet = bett(INITIAL_MONEY);





从一开始就要学习为变量使用合理的名称。将它们命名为x,y和z是一个混淆的秘诀。

 x可以被称为initialMoney 
y被称为riskedMoney



由于这是C,您需要在main()函数之前为函数定义原型。或者将主函数放在最后。

  //  使用函数的描述性名称 
int GetBetFromPlayer( int initialMoney);

int main()
{
}





在while循环中,你只有一个条件,你需要两个正确。

你需要检查riskedMoney是否在上限和下限之内。

所以

 (y> x)



应该是

 ((riskedMoney< MIN_BET) &&(riskedMoney> initialMoney))



你可能会记下你认为哪种说法更具可读性。

Don'害怕使用长名称和空格来使你的代码更容易阅读。



在那里,我认为这会让你暂停一段时间。


i am working on a blackjack game , i am still a biganner and i am having some problems with writing functions .
i want to have something like this ...

for example:

Welcome to CS intro Blackjack!
Start by entering a random seed:
5
You currently have 1000 fake dollars. How much do you want to bet?
1300
our bet must be at least 1 and at most 1000, try again:
0
Your bet must be at least 1 and at most 1000, try again:
1000



What I have tried:

int main()
{
  int money=0;
  int seed=0;
  int bet=0;
  printf("Welcome to CS intro Blackjack!\n");
  printf("Start by entering a random seed:\n");
  scanf("%d",&seed);
  bet=bett(money);

  return 0;
}
int bett(int x)
  {
      int y=0;
      printf("You currently have %d fake dollars. How much do you want to bet?",x);
      scanf("%d",&y);
      while(y>x)
      {
         printf("Your bet must be at least 1 and at most %d, try again:",x);
          scanf("%d",&y);
      }
      return(y);
  }

解决方案

First you have to figure the logic of your program.
I guess this is the first assignment in a school project, so not too complicated yet.

First you have two input limitations: the entered bet must be more than 1 and less than available funds.
Hard coded values is always a bad idea, so start with declaring a constant for the lower limit:
For example

#define MIN_BET 1



If you look at function bett(int x), your logic there is not correct.
You call the function with the variable money, but you have initialized money to zero.
You need to assign an initial value to money first and this should also be a constant.

#define INITIAL_MONEY 1000



then assign

money = INITIAL_MONEY;
bet = bett(money);
or
bet = bett(INITIAL_MONEY);



Also learn from the beginning to use reasonable names for your variables. Naming them x, y and z is a recipe for confusion.

x could be called initialMoney
y could be called riskedMoney


As this is C, you need to define a prototype for your functions before the main() function. Or put the main function last.

// Use descriptive names for functions as well
int GetBetFromPlayer(int initialMoney);

int main()
{
}



In the while loop, you only have one condition, and you need two right.
You need to check that the riskedMoney is within both the upper and lower limits.
so

while(y>x)


should be

while ((riskedMoney < MIN_BET) && (riskedMoney > initialMoney))


You might make a note of which statement you think is more readable.
Don't be afraid of using long names and spaces to make your code more easy to read.

There, I think this will keep you occupied for a while.


这篇关于我需要弄清楚如何解决这个问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆