Javascript编写程序:通过计算机随机产生10道四则运算题,两个操作数为1~10之间的随机数,

2024-11-22 07:57:25
推荐回答(3个)
回答(1):


    
        
    
    
        
            
            

                
                
                
                
        
=确定


        

            var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
            var operators = ['+', '-', '*', '/'];
            var rightNum = 0;
            var wrongNum = 0;
            var count = 0;
            var num1;
            var num2;
            var operator;
            randomQuestion();

            function randomQuestion(){
                if(count > 9){
                    if(count <= 10){
                        calculateResult();
                        alert("总分:"+rightNum*10+"\n答错"+wrongNum+"题");
                    }
                    count++;
                    return;
                }
                if(count > 0){
                    calculateResult();
                }
                num1 = numbers[Math.floor(Math.random()*10)];
                num2 = numbers[Math.floor(Math.random()*10)];
                operator = operators[Math.floor(Math.random()*4)]
                question.innerText = num1+operator+num2;
                count++;
                title.innerText = "第"+count+"题";
                answer.value = "";
            }

            function calculateResult(){
                var result;
                switch(operator){
                    case '+':
                        result = num1+num2;
                        break;
                    case '-':
                        result = num1-num2;
                        break;
                    case '*':
                        result = num1*num2;
                        break;
                    case '/':
                        result = num1/num2;
                        break;
                    default:
                        break;
                }
                if(parseFloat(answer.value).toFixed(3) == parseFloat(result).toFixed(3)){
                    rightNum++;
                    alert("Right");
                }else{
                    wrongNum++;
                    alert("Wrong");
                }
            }
        
    

除不尽时答案保留3位小数。

回答(2):

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
">

http://www.w3.org/1999/xhtml
">



    

    MyCardPurchaseSuccess



















刷新



包括小数点只能输入4位,保留两位小数,输入框失去焦点就算出正确与否并锁定,全部锁定算出总分。

另外还是觉得你这么直接伸手要代码是不好的。正好我也在学习练练手,就发给你了

回答(3):

自己编写呗~这样赤果果的伸手要程序啊~你自行编写,不会的在提问吧~

相关问答
最新问答