2个骰子随机抛出100× [英] 2 dices randomly thrown 100×

查看:111
本文介绍了2个骰子随机抛出100×的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写一个程序,随机扔掉这对骰子100次,然后计算:总共4得分多少次。11得分多少次。总共少于6次是多少次抛出。



我尝试了什么:



  #include   <   stdio.h  >  
#include < stdlib.h >
int main( void
{
int dice_a,dice_b,tot_4,tot_11,tot_6;
double i;
dice_a = rand()% 6 + 1 ;
dice_b = rand()% 6 + 1 ;
执行
{
printf( %d \ n,i);
} while (i< = 100 );
tot_4 = dice_a + dice_b;
tot_11 = dice_a + dice_b;
tot_6 = dice_a + dice_b;
if (tot_4 == 4
printf( 总共抛出4次的次数=%d \ n,tot_4);
else if (tot_11 == 11
printf( 总共抛出11次的次数=%d \ n ,tot_11);
else if (tot_6< 6
printf( 总共小于6的次数=%d \ n,tot_6);
return 0 ;
}

解决方案

你只掷一次骰子,这样你的答案就毫无意义了。您还无缘无故地打印数字1到100。然后通过将两个初始骰子值加在一起来计算每个总数。



您需要逻辑思考实现此目的所需的步骤,而不是考虑C代码:



在循环中,您需要执行以下操作:



  • 投掷两个骰子,即为每个骰子调用rand()。
  • 将两个值加在一起。
  • 然后


    • 如果等于4则将1添加到tot_4。
    • 如果等于11则将1添加到tot_11
    • 如果它们小于6则将1添加到tot_6


  • 重复100次
  • 打印总计


我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不打算为你做这一切!


你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码正在做什么和你的任务是与它应该做的比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是帮助你通过向您展示正在发生的事情。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试你的程序(踩踏和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。

Write a program to randomly throw the pair of dice 100 times, and to count:How many times a total of "4" was scored.How many times "11" was scored.How many times a total less than 6 was thrown.

What I have tried:

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int dice_a, dice_b, tot_4, tot_11, tot_6;
    double i;
    dice_a= rand() % 6 + 1;
    dice_b= rand() % 6 + 1;
   do
    {
        printf("%d\n", i);
    } while (i <= 100);
    tot_4= dice_a + dice_b;
    tot_11= dice_a + dice_b;
    tot_6= dice_a + dice_b;
    if (tot_4 == 4)
        printf("Number of times a total of 4 was thrown = %d\n", tot_4);
    else if (tot_11 == 11)
        printf("Number of times a total of 11 was thrown = %d\n", tot_11);
    else if (tot_6 < 6)
        printf("Number of times a total less than 6 was thrown = %d\n", tot_6);
    return 0;
}

解决方案

You only throw the dice once so your answers will be meaningless. You also print the numbers 1 to 100 for no reason. You then calculate each total by adding the two initial dice values together.

You need to think logically about the steps required to achieve this, rather than thinking about the C code:

In the loop you need to do the following:

  • Throw the two dice, i.e call rand() for each one.
  • Add the two values together.
  • Then

    • if they equal 4 add 1 to tot_4.
    • if they equal 11 add 1 to tot_11
    • if they are less than 6 add 1 to tot_6

  • repeat 100 times
  • print the totals


We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于2个骰子随机抛出100×的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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