【问题标题】:Generating Random numbers and printing "Done!" if they fall within a range in C生成随机数并打印“完成!”如果它们在 C 的范围内
【发布时间】:2014-09-30 05:02:43
【问题描述】:

抱歉,如果此问题已在其他地方得到解答,我进行了搜索,但找不到我要查找的内容。

无论如何,我被一个大学作业问题困住了,这个问题要求我创建一个脚本,该脚本随机生成 0-99 之间的数字并每次在新行上打印数字,如果刚刚打印的数字落在范围 68-74 它应该打印完成!在下一行并退出脚本。

我得到了一个模板,其中大部分代码已经为我完成,它看起来像这样:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand(time(NULL));
    /**
     * Your solution must start from below this point. No code modifications WHATSOEVER are allowed ABOVE this point!
     */


    /**
     * Your solution must have finished by this point. No code modifications WHATSOEVER are allowed BELOW this point!
     */
    return 0;
}

int getRandInt() {
    return rand() % 100;
}

无论如何,经过一番折腾,我终于让它打印一个随机数列表,而不是无限地打印 1 个随机数。我是这样做的:

do
    printf ("%d\n", getRandInt());
while (getRandInt());

我尝试插入以下内容:

do
    printf ("%d\n", getRandInt());
while (getRandInt() <68);

看看它是否至少只打印小于 68 的随机整数,但这只是让它只打印 1 或 2 个随机数(有时值大于 68),而不是之前的代码块打印的巨大列表。

我是否调用了正确的函数?我应该使用 Do While 循环吗?如何设置循环退出并打印“完成!”的数字范围?显然我不能在 cmets 之外编辑代码。

我对编码非常陌生,如果能在此问题上提供任何帮助,我将不胜感激。

【问题讨论】:

  • 你做了多少实验?
  • 很多,我整个上午都在插入不同的东西。
  • 现在希望你的教授不要也来这里!

标签: c loops random srand


【解决方案1】:

每次调用getRandInt() 时,都会得到一个新的随机值。这意味着您正在使用与打印不同的随机数检查 while 循环中的条件。

要解决此问题,您需要获取一个随机值并将其存储,然后进行比较。您可以使用多种不同类型的循环(包括 do/while)来解决此问题。

还要注意,在 do 语句中使用父母是一种很好的风格:

do {
    // code here.
} while ( ... );

由于您已将此确定为家庭作业,因此除非您有其他问题,否则我会将帮助留在那里。

【讨论】:

  • 所以我应该声明另一个像“number”这样的 int 然后执行类似的操作: int number = getRandInt() 然后在循环中调用 number int 吗?
【解决方案2】:

声明另一个整数变量并在循环内调用函数。所以每次将返回值存储在该变量中时,使用该变量进行条件检查。

试试下面的代码sn-p-

/**
 * Your solution must start from below this point. No code modifications WHATSOEVER are       allowed ABOVE this point!
 */
    int ret;
    do{
            ret = getRandInt();
            printf("%d\n",ret);

    }while(!(ret >= 68 && ret <= 74));

    printf("Done!\n");
/**
 * Your solution must have finished by this point. No code modifications WHATSOEVER are allowed BELOW this point!
 */

从上面的代码中,当数字落在 68 到 74 之间时,它将打印Done! 并退出循环。如果不是 (!(ret &gt;= 68 &amp;&amp; ret &lt;= 74)),它将继续执行循环,直到某个数字落在 68 到 74 之间。

【讨论】:

  • 谢谢你,这项工作完美!最重要的是,我了解它为什么有效。我试图声明另一个变量,但我没有在循环中这样做,所以我不断得到相同的随机数重复。我也不知道“&&”命令。再次感谢您,感谢所有评论的人,非常感谢!
  • @PermanentWaves &amp;&amp; 这是逻辑和运算符。当左侧和右侧条件都为真时,它将返回真。
  • 带有exitif 条件毫无意义:您可以将Done! 打印移动到while 条件之后。
  • @Sathish 我记得 && 现在来自 MATLAB,我看到其他人使用 while(r > 74 || r
  • @PermanentWaves 是的。你说的对!。你可以找到很多这样的方法!
【解决方案3】:

我猜你只是对如何在do while 循环中编写条件感到困惑。

试试这个代码吧!

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int getrandom_no()
{
    return rand() % 100;
}

int main(void) {
    int r=0;

    srand(time(NULL)); /* To use rand function you must use srand first */
    do 
    {
        r = getrandom_no();
        printf("%d\n",r);
    }while(r > 74 || r < 68);
    puts("Done!");

    return 0;
}

【讨论】:

  • 这个脚本可以工作,但我不能编辑 cmets 之外的任何代码。无论如何,谢谢你,因为我要研究的例子越多,我的理解就越好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-24
  • 1970-01-01
  • 1970-01-01
  • 2010-10-20
  • 2018-04-08
  • 1970-01-01
相关资源
最近更新 更多