欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js实现随机的四则运算题目效果

程序员文章站 2023-11-21 17:42:16
本文主要介绍了随机的四则运算题目,这样就可以自动的生成随机的30个四则运算题目了。可以生成随机的四则运算题目给给小学生用,但是还是有问题,小学生啊!他们不知道负数,不知道小...

本文主要介绍了随机的四则运算题目,这样就可以自动的生成随机的30个四则运算题目了。可以生成随机的四则运算题目给给小学生用,但是还是有问题,小学生啊!他们不知道负数,不知道小数。所以还要加入判定条件。具体代码如下:

switch(arr[n]){
    case "+":{
    sum=a+b;
    break;
    }
    case "-":{
    sum=a-b;
    while(sum<0){
     var a = getrandomnum(1,30);
     var b = getrandomnum(1,30);
     sum=a-b;
    }
    break;
    }
    case "*":{
    sum=a*b;
    break;
    }
    case "/":{
    sum=a/b;
    while(hasdot(sum)){
      var a = getrandomnum(1,100);
      var b = getrandomnum(1,100);
      sum=a/b;
     }
    break;
    }

这样负数和小数都没有了。

 不过,小学生貌似会分数的运算。于是我又开始试试写分数的。
 

<body> 
<button class="btn btn-success btn-sm" type='button' onclick="pl_f()">开始生成题目</button>
 <span id="text_f"></span>
 <script>
 function getrandomnum(min,max)
 { 
  var range = max - min; 
  var rand = math.random(); 
  return(min + math.round(rand * range)); 
 }
 function max_num(x,y) {
  var t;
  while (y) {
   t = x%y;
   x = y;
   y = t;
  }
  return x;
 }
 function fenshu(a,b,p) {
  if (p==1) {
   if (a>b) {
    return a+"/"+b;
   }else if(a<b){
    return a+"/"+b;
   }else {
    return 1;
   }
  }else {
   var x=a/p;
   var y=b/p;
   if(y==1){
    return x;
   }else{
    return x+"/"+y;
   }
  }
 } 
 function pl_f() {
  var son_a = getrandomnum(1,30); 
  var par_b = getrandomnum(1,30);
  var son_c = getrandomnum(1,30); 
  var par_d = getrandomnum(1,30); 
  var x=son_a+"/"+par_b;
  var y=son_c+"/"+par_d;
  document.write("第一个分数:"+x+"<br>");
  document.write("第二个分数"+y+"<br>");
  max_x=max_num(son_a,par_b);
  max_y=max_num(son_c,par_d);
  document.write("第一个最大公约数"+max_x+"<br>");
  document.write("第二个最大公约数"+max_y+"<br>");
  var n=fenshu(son_a,par_b,max_x);
  var m=fenshu(son_c,par_d,max_y);
  document.write("第一个分数化简:"+n+"<br>");
  document.write("第二个分数化简"+m);
 }
 </script>
 </body>

 这个可以动态的生成两个分数,然后输出这个分数分子和分母的最大公约数。最后输出分子的最简式。

最后就是将分数放入整数一起了!

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title> new document </title>
 <meta http-equiv="content-type" content="text/html; charset=utf-8">
 <meta name="generator" content="editplus" />
 <meta name="author" content="" />
 <meta name="keywords" content="" />
 <meta name="description" content="" />
 </head>

 <body>
 <button type='button' onclick="play_all()">开始生成题目</button>
 <span id="text"></span>
 </body>
 <script>
 function getrandomnum(min,max)
 { 
  var range = max - min; 
  var rand = math.random(); 
  return(min + math.round(rand * range)); 
 }
 function max_num(x,y) {
  var t;
  while (y) {
   t = x%y;
   x = y;
   y = t;
  }
  return x;
 }
 function fenshu(a,b,p) {
  if (p==1) {
   if (a>b) {
    return a+"/"+b;
   }else if(a<b){
    return a+"/"+b;
   }else {
    return 1;
   }
  }else {
   var x=a/p;
   var y=b/p;
   if(y==1){
    return x;
   }else{
    return x+"/"+y;
   }
  }
 }
 function f_count(a,b,c,d) {
   var arr = ["+","-","*","/"]; 
   var n = getrandomnum(0,3);
   switch(arr[n]){
    case "+":{
    var sum_x=(a*d)+(b*c);
    var sum_y=b*d;
    max_x=max_num(sum_x,sum_y);
    max_y=max_num(sum_x,sum_y);
    var n=fenshu(sum_x,sum_y,max_x);
    return a+"/"+b+"+"+c+"/"+d+"="+n;
    break;
    }
    case "-":{
    var sum_x=(a*d)-(b*c);
     while (sum_x<0) {
      var a = getrandomnum(1,30); 
      var b = getrandomnum(1,30);
      var c = getrandomnum(1,30); 
      var d = getrandomnum(1,30);
      var sum_x=(a*d)-(b*c);
     }
    var sum_y=b*d;
    max_x=max_num(sum_x,sum_y);
    max_y=max_num(sum_x,sum_y);
    var n=fenshu(sum_x,sum_y,max_x);
    return a+"/"+b+"-"+c+"/"+d+"="+n;
    break;
    }
    case "*":{
    var sum_x=a*c;
    var sum_y=b*d;
    max_x=max_num(sum_x,sum_y);
    max_y=max_num(sum_x,sum_y);
    var n=fenshu(sum_x,sum_y,max_x);
    return a+"/"+b+"*"+c+"/"+d+"="+n;
    break;
    }
    case "/":{
    var sum_x=a*d;
    var sum_y=b*c;
    max_x=max_num(sum_x,sum_y);
    max_y=max_num(sum_x,sum_y);
    var n=fenshu(sum_x,sum_y,max_x);
    return a+"/"+b+"÷"+c+"/"+d+"="+n;
    break;
    }
   }
 }
 function count(a,b){
   var arr = ["+","-","*","/"]; 
   var n = getrandomnum(0,3);
   switch(arr[n]){
    case "+":{
    var sum=a+b;
    return a+"+"+b+"="+sum;
    break;
    }
    case "-":{
    var sum=a-b;
    while(sum<0){
     var a = getrandomnum(1,30);
     var b = getrandomnum(1,30);
     var sum=a-b;
    }
    return a+"-"+b+"="+sum;
    break;
    }
    case "*":{
    var sum=a*b;
    return a+"*"+b+"="+sum;
    break;
    }
    case "/":{
    var sum=a/b;
    while(a%b!==0){
     var max_x=max_num(a,b);
     var n=fenshu(a,b,max_x);
     return a+"÷"+b+"="+n;
    }
    return a+"÷"+b+"="+sum;
    break;
    }
   }
 }
 function pl_f() {
  var son_a = getrandomnum(1,30); 
  var par_b = getrandomnum(1,30);
  var son_c = getrandomnum(1,30); 
  var par_d = getrandomnum(1,30);
  var suma=f_count(son_a,par_b,son_c,par_d);
  return suma;
 }
 function pl() {
   var a = getrandomnum(1,30); 
   var b = getrandomnum(1,30);
   var sumb=count(a,b);
   return sumb;
 }
 function play_all() {
   for (i = 1; i <=30; i++) {
    var a = getrandomnum(0,1);
    document.getelementbyid("text").innerhtml+=("<br>"+"第"+i+"题:");
    switch (a) {
     case 0:
      var a=pl();
      document.getelementbyid("text").innerhtml+=(a);
      break;
     case 1:
      var b=pl_f();
      document.getelementbyid("text").innerhtml+=(b);
      break;
    }
   }
 }
 </script>
</html>

执行完节目就成这样的啦

js实现随机的四则运算题目效果

最后上个答案和题目分开的版本!

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title> new document </title>
 <meta http-equiv="content-type" content="text/html; charset=utf-8">
 <meta name="generator" content="editplus" />
 <meta name="author" content="" />
 <meta name="keywords" content="" />
 <meta name="description" content="" />
 </head>

 <body>
 <button type='button' onclick="play_all()">开始生成题目</button>
 <span id="text"></span>
 <span id="text2"></span>
 </body>
 <script>
 function getrandomnum(min,max)
 { 
  var range = max - min; 
  var rand = math.random(); 
  return(min + math.round(rand * range)); 
 }
 function max_num(x,y) {
  var t;
  while (y) {
   t = x%y;
   x = y;
   y = t;
  }
  return x;
 }
 function fenshu(a,b,p) {
  if (p==1) {
   if (a>b) {
    return a+"/"+b;
   }else if(a<b){
    return a+"/"+b;
   }else {
    return 1;
   }
  }else {
   var x=a/p;
   var y=b/p;
   if(y==1){
    return x;
   }else{
    return x+"/"+y;
   }
  }
 }
 function f_count(a,b,c,d) {
   var arr = ["+","-","*","/"]; 
   var n = getrandomnum(0,3);
   switch(arr[n]){
    case "+":{
    var sum_x=(a*d)+(b*c);
    var sum_y=b*d;
    max_x=max_num(sum_x,sum_y);
    max_y=max_num(sum_x,sum_y);
    var n=fenshu(sum_x,sum_y,max_x);
    return a+"/"+b+"+"+c+"/"+d+"="+n;
    break;
    }
    case "-":{
    var sum_x=(a*d)-(b*c);
     while (sum_x<0) {
      var a = getrandomnum(1,30); 
      var b = getrandomnum(1,30);
      var c = getrandomnum(1,30); 
      var d = getrandomnum(1,30);
      var sum_x=(a*d)-(b*c);
     }
    var sum_y=b*d;
    max_x=max_num(sum_x,sum_y);
    max_y=max_num(sum_x,sum_y);
    var n=fenshu(sum_x,sum_y,max_x);
    return a+"/"+b+"-"+c+"/"+d+"="+n;
    break;
    }
    case "*":{
    var sum_x=a*c;
    var sum_y=b*d;
    max_x=max_num(sum_x,sum_y);
    max_y=max_num(sum_x,sum_y);
    var n=fenshu(sum_x,sum_y,max_x);
    return a+"/"+b+"*"+c+"/"+d+"="+n;
    break;
    }
    case "/":{
    var sum_x=a*d;
    var sum_y=b*c;
    max_x=max_num(sum_x,sum_y);
    max_y=max_num(sum_x,sum_y);
    var n=fenshu(sum_x,sum_y,max_x);
    return a+"/"+b+"÷"+c+"/"+d+"="+n;
    break;
    }
   }
 }
 function count(a,b){
   var arr = ["+","-","*","/"]; 
   var n = getrandomnum(0,3);
   switch(arr[n]){
    case "+":{
    var sum=a+b;
    return a+"+"+b+"="+sum;
    break;
    }
    case "-":{
    var sum=a-b;
    while(sum<0){
     var a = getrandomnum(1,30);
     var b = getrandomnum(1,30);
     var sum=a-b;
    }
    return a+"-"+b+"="+sum;
    break;
    }
    case "*":{
    var sum=a*b;
    return a+"*"+b+"="+sum;
    break;
    }
    case "/":{
    var sum=a/b;
    while(a%b!==0){
     var max_x=max_num(a,b);
     var n=fenshu(a,b,max_x);
     return a+"÷"+b+"="+n;
    }
    return a+"÷"+b+"="+sum;
    break;
    }
   }
 }
 function pl_f() {
  var son_a = getrandomnum(1,30); 
  var par_b = getrandomnum(1,30);
  var son_c = getrandomnum(1,30); 
  var par_d = getrandomnum(1,30);
  var suma=f_count(son_a,par_b,son_c,par_d);
  return suma;
 }
 function pl() {
   var a = getrandomnum(1,30); 
   var b = getrandomnum(1,30);
   var sumb=count(a,b);
   return sumb;
 }
 function play_all() {
   for (i = 1; i <=30; i++) {
    var a = getrandomnum(0,1);
    document.getelementbyid("text").innerhtml+=("<br>"+"第"+i+"题:");
    switch (a) {
     case 0:
      var a=pl();
      var strs = a.split("=");
      document.getelementbyid("text").innerhtml+=(strs[0]+"="+"<br>");
      document.getelementbyid("text2").innerhtml+=("<br>"+"第"+i+"题答案:"+strs[1]);
      break;
     case 1:
      var b=pl_f();
      var strs = b.split("=");
      document.getelementbyid("text").innerhtml+=(strs[0]+"="+"<br>");
      document.getelementbyid("text2").innerhtml+=("<br>"+"第"+i+"题答案:"+strs[1]);
      break;
    }
   }
 }
 </script>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。