帮助我的练习计划 [英] Help with my practice program

查看:64
本文介绍了帮助我的练习计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。谁能告诉我我做错了什么?我实际上是根据我正在研究的一本书编写代码。但每次运行都没有一个数字。



 #include< cstdlib> 
#include< ctime>
#include< iostream>

使用 命名空间标准;

int randRange( int MIN, int MAX);

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

randRange( 15 20 );
}

int randRange( int MIN, int MAX)
{
while true
{
int final_rand = rand();
if (final_rand> = MIN&& final_rand< = MAX)
{
return final_rand;

}
}
}





我尝试过:



我试过在if语句中添加break;但仍然没有输出。

解决方案

那可能是因为你生成它后你没有对值做任何事情。

所有你要做的就是调用函数,它返回一个值 - 但是你不要尝试将它存储在任何地方,或者将其输出给用户,然后关闭你的应用程序。

也许你想尝试这样做:

< pre lang =c ++> cout<< randRange( 15 20 );

这可能会有所帮助。

但它没有多大帮助,因为生成一个适合你的窄范围的随机数可能需要很长时间。 rand 返回介于0和RAND_MAX之间的值(取决于系统,但至少为32767)。

如果查看函数定义: rand - C ++参考 [ ^ ]它显示了一种减少输出范围的更好方法。所以在你的情况下,取出返回值的模数(MAX-MIN),然后将它偏移MIN,你根本不需要循环!


如果你是不怕使用 C ++ 11 功能,有< random>< / random> 标题:< a href =http://www.cplusplus.com/reference/random/>随机参加C ++参考 [ ^ ]


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

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

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



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你已经接近了一个错误。


Hi there. Can anyone tell me what I did wrong? I actually based the code on a book I'm studying on. But there's not a single digit every time I run it.

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int randRange(int MIN, int MAX);

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

    randRange(15, 20);
}

int randRange(int MIN, int MAX)
{
    while (true) 
    {
        int final_rand = rand();
        if (final_rand>=MIN && final_rand<=MAX)
        {
            return final_rand;
            
        }
    }
}



What I have tried:

I have tried putting a " break; " inside the if statement, but still no output.

解决方案

Well...that's probably because you don't do anything with the value once you've generated it.
All you do is call the function, which returns a single value - but you don;t try to store it anywhere, or output it to the user, and your application then closes.
Perhaps you want to try this instead:

cout << randRange(15, 20);

which might help.
But it won't help much, because it's probably going t o take quite a while to generate a random number that fits into your narrow range. rand returns a value between 0 and RAND_MAX (which is system dependant, but which will be at least 32767).
If you look at the function definition: rand - C++ Reference[^] it shows a better way to reduce teh range of outputs. So in your case, take the modulus of the returned value (MAX-MIN) and then offset it by MIN and you don't need the loop at all!


And if you're not scared of using C++11 features, there's the <random></random> header: random at C++ Reference[^]


You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于帮助我的练习计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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