js如何产生一个100以内的随机数,如果产生的数小于10则重新产生,直到得到符合条件的数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js如何产生一个100以内的随机数,如果产生的数小于10则重新产生,直到得到符合条件的数相关的知识,希望对你有一定的参考价值。
参考技术A <html ><head>
<title></title>
<script type="text/javascript">
function bb()
var ccc=parseInt(100*Math.random());
alert(ccc);
for(i=10;i>ccc;ccc=parseInt(100*Math.random()))
alert(ccc);
document.getElementById("eeee").value=ccc;
alert(ccc);
</script>
</head>
<body>
<tr><td><input type="text" name="eeee" onBlur="bb()"/></td></tr>
</table>
</form>
</body>
</html>
你修改下就ok了 参考技术B <script>
var a = 0;
do
a = Math.floor(Math.random()*101);
while(a<10)
document.write(a);
</script> 参考技术C var r=[];//结果
for(var i=0;i<100;i++)
r[i]=10+parseInt(Math.random()*90);
本回答被提问者采纳
猜数程序.要完整的程序!(用rand产生一个随机数)
若猜的数比产生的数小或大,提示并重猜;直到猜中为止.
拜托了!
要用C语言的
第2种也不可行啊!
: fatal error C1004: unexpected end of file found
第3种也不可行~~ 在猜之前就已经把这个数显示出来了!!
#include<stdlib.h>
#include<time.h>
void
main()
int
m,s,t,flag;
char
ch;
for(;;)
flag=0;
s=0;
srand(time(0));
do
m=rand();
while(m<0||m>100);
cout<<"我已经想到了一个0-100的整数,请你猜猜看。"<<endl;
for(;;)
if(flag)break;
cin>>t;
s++;
if(t>m)
cout<<"你猜的数太大了。"<<endl;
else
if(t<m)
cout<<"你猜的数太小了。"<<endl;
else
cout<<"恭喜!你猜对了!你猜了"<<s<<"次"<<endl;
cout<<"还想玩吗?(Y/N)";
cin>>ch;
if(!(ch=='Y'||ch=='y'))
return;
else
flag=1;
参考技术A 以下程序的功能是随机产生数字,要求用户猜测程序中产生的随机数字,并输入,根据猜测的结果程序给出不同的响应,如果15次没猜对则退出。
源程序如下:
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
main()
int count;/*猜数字的次数*/
int number;/*系统产生的随机数字*/
int guess;/*程序员输入数字*/
char yes='Y';
clrscr();
printf("\nNow let us play the game.\n Guess the number:");
while (toupper(yes)=='Y')
count=0;
randomize();
number=random(100)+1;
do
do
printf("\nInput an integer number(1~100):");
scanf("%d",&guess);
while(!(guess>=1&&guess<=100));/*结束第二层DO~WHILE循环*/
if (guess<number)
printf("\n Your answer is low,try again!");/*如果用户输入的数字小于系统随机数,则输出数字太小的提示信息*/
if (guess>number)
printf("\n Your answer is high,try again!");/*如果用户输入的数字大于系统随机数,则输出数字太小的提示信息*/
count++;/*猜测次数加一*/
if (count==15)
printf("\n This is the %d times! Think it hard next!",count);
exit(0);/*如猜测15次还没猜对,则退出游戏*/
while (!(guess==number));
if (count<=7)/*猜测的次数小于7次*/
printf("\n You have got it in %d times.\n",count);
printf("\n you guess right,Congretulations!");/*游戏成功则提示祝贺信息*/
else
printf("\n You got it in %d times.\n",count);
printf("\n I bet you can do it better!");/*游戏失败则提示鼓励信息*/
printf("\n NEXT?(Y/N):");/*选择是否重新游戏*/
scanf("%c",&yes);
运行程序时请用户猜数字,该数字由系统随机产生,用户最多有七次猜测的机会,如果在七次内猜对数字,则程序显示祝贺信息,如果用户大于七次猜对数字,则程序显示鼓励信息,如果用户连续15次都没有猜对数字,则游戏自动退出。结束一次游戏后,系统询问用户进行下一次猜数字游戏,用户输入“Y”则开始下一次猜数字游戏,用户如果输入“N”则退出游戏。
唉,花了个把小时终于搞出来了,应可以看懂吧,程序中作了很详细的解释,且在最后也作了功能说明!
如果认为好的话,追加分数哦! 参考技术B 我试了一下,可以啊,没什么问题啊,你用的是什么编译器啊?你确定你没漏了什么符号啊!!!
#include "stdio.h"
#include "math.h"
void main()
int t,i,j=0;
i=rand();
printf("请输入你猜的数:");
loop:scanf("%d",&t);
j+=1;
if(t<i)
printf("猜的数比产生的数小\n请从新输入:"); goto loop;
else if(i<t)
printf("猜的数比产生的数大\n请从新输入:"); goto loop;
else printf("\n恭喜!你猜对了!你猜了%d次",j);
试试这个:
#include "stdio.h"
#include "math.h"
void main()
int t,i,j=0;
i=rand();
printf("请输入你猜的数:");
for(;;)
scanf("%d",&t);
j+=1;
if(t<i)
printf("猜的数比产生的数小\n请从新输入:"); continue;
else if(i<t)
printf("猜的数比产生的数大\n请从新输入:"); continue;
else
printf("\n恭喜!你猜对了!你猜了%d次",j);break;
参考技术C 这个程序拿去看看,有什么不满意的提出来
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
int i ,guess=0;
srand(time(0)); //初始化随机种子
i = rand()%100;
printf("%d\n",i);
printf("请你输入一个数(1-100)\n");
scanf("%d",&guess);
while(guess != i)
if(guess < i )
printf("请你输入一个比%d大的数",guess);
scanf("%d",&guess);
else
printf("请你输入一个比%d小的数",guess);
scanf("%d",&guess);
printf(" 恭喜你,打对了!\n");
参考技术D #include<iostream.h>
#include<stdlib.h>
#include<time.h>
void main()
int m,s,t,flag;
char ch;
for(;;)
flag=0;
s=0;
srand(time(0));
do
m=rand();
while(m<0||m>100);
cout<<"我已经想到了一个0-100的整数,请你猜猜看。"<<endl;
for(;;)
if(flag)break;
cin>>t;
s++;
if(t>m)
cout<<"你猜的数太大了。"<<endl;
else if(t<m)
cout<<"你猜的数太小了。"<<endl;
else
cout<<"恭喜!你猜对了!你猜了"<<s<<"次"<<endl;
cout<<"还想玩吗?(Y/N)";
cin>>ch;
if(!(ch=='Y'||ch=='y'))
return;
else
flag=1;
本回答被提问者采纳
以上是关于js如何产生一个100以内的随机数,如果产生的数小于10则重新产生,直到得到符合条件的数的主要内容,如果未能解决你的问题,请参考以下文章
Python随机产生10个70-100的数并输出,找出其中的最小值及其第一次出现的位置?
Python 随机产生[0,100]以内的随机数,找到最大值和最小值并交换位置