【发布时间】: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 之外编辑代码。
我对编码非常陌生,如果能在此问题上提供任何帮助,我将不胜感激。
【问题讨论】:
-
你做了多少实验?
-
很多,我整个上午都在插入不同的东西。
-
现在希望你的教授不要也来这里!