用js写的话,不是很简单嘛,就是你在input 里输入一个数,点确定的时候生成一个随机数,和你输入的数字比较,如果一样就猜对了呗,至于难度,就是写三个方法,简单的就是生成整数的方法,你选择不同难度的时候,给button 改变点击事件就行
大体思路是这样的,我下班帮你写一个。
var random1 = parseInt(Math.random()*5);
var random2 = parseInt(Math.random()*50);
var random3 = parseInt(Math.random()*100);
function simple(){
var inputVal = $("#input").val();
if(inputVal==""){
alert("请输入数字啊,混蛋!!!");
return ;
}
if(random1==inputVal){
if(confirm("猜对了,是否进行下一局游戏")){
window.location.reload();
}
}else if(random1>inputVal){
$("#ErrorMessage").text("猜小了,再来~~");
}else{
$("#ErrorMessage").text("猜大了,再来~~");
}
}
function General(){
var inputVal = $("#input").val();
if(inputVal==""){
alert("请输入数字啊,混蛋!!!");
return ;
}
if(random2==inputVal){
if(confirm("猜对了,是否进行下一局游戏")){
window.location.reload();
}
}else if(random2>inputVal){
$("#ErrorMessage").text("猜小了,再来~~");
}else{
$("#ErrorMessage").text("猜大了,再来~~");
}
}
function Difficult(){
var inputVal = $("#input").val();
if(inputVal==""){
alert("请输入数字啊,混蛋!!!");
return ;
}
if(random3==inputVal){
if(confirm("猜对了,是否进行下一局游戏")){
window.location.reload();
}
}else if(random3>inputVal){
$("#ErrorMessage").text("猜小了,再来~~");
}else{
$("#ErrorMessage").text("猜大了,再来~~");
}
}
function selectChange(){
var value=$("#select").val();
if(value==1){
$("#submit").attr("onclick","simple()");
}else if(value==2){
$("#submit").attr("onclick","General()");
}else{
$("#submit").attr("onclick","Difficult()");
}
}
编程实现一个‘猜数’游戏,从键盘输入数字进行猜测,计算机给出相应提示,所猜数字过大或过小。总共有20次猜数的机会。(由书上第五章课后习题改变而来)
源程序:
#include
#include
#include
void main()
{
int a,b,i;
b=rand()%100,
i=1;
printf("====This is a Number Guess Game!====\n You can guess a number between 1~100,and you only have 20 chances.\n Good luck to you!\n");
for(i=1;i<=20;)
{
printf("Please input a number:\n");
scanf("%d",&a);
if(a<0||a>100)
{
printf("You have guess %d times!\n",i);
printf("This is a wrong number,please input a number between 1~100.\n");i++;getchar();
}
else if(a==b)
{
printf("You have guess %d times!\n",i);
printf("You are luck!\n");getchar();break;
}
else if(a {
printf("You have guess %d times!\n",i);
printf("Sorry,a little smaller!Please try again!\n");i++;getchar();
}
else
{
printf("You have guess %d times!\n",i);
printf("Sorry,a little bigger!Please try again!\n");i++;getchar();
}
}
if(i>20)printf("The number is %d!\n",b);
printf("Thank you for using!Welcome to using again!Bye Bye!\n ");
}