Javascript编写程序:通过计算机随机产生10道四则运算题,两个操作数为1~10之间的随机数,
编写程序:通过计算机随机产生10道四则运算题,两个操作数为1~10之间的随机数,运算类型为随机产生的加,减,乘,整除中的任意一种,如果输入答案正确,则显示"Right!"...
编写程序:通过计算机随机产生10道四则运算题,两个操作数为1~10之间的随机数,运算类型为随机产生的加,减,乘,整除中的任意一种,如果输入答案正 确,则显示"Right!",否则显示"Wrong!",不给机会重做,10道题做完后,按每题10分统计总得分,然后打印出总分和做错的题目数量.
展开
3个回答
展开全部
<html>
<head>
</head>
<body>
<table>
<tr><td colspan="4"><label id="title" /></td>
<tr>
<td><label id="question" style="width: 120px;height: 25px;border: 1px solid #000000;text-align: center;vertical-align: middle;line-height: 22px;" /></td>
<td>=</td>
<td><input id="answer" style="width: 50px;height: 25px;text-align: center;vertical-align: middle;line-height: 20px;" /></td>
<td><button style="width: 50px;height: 25px;vertical-align: middle;line-height: 20px;" onclick="randomQuestion()">确定</button></td>
</table>
<script type="text/javascript">
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");
}
}
</script>
</body>
</html>
除不尽时答案保留3位小数。
展开全部
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MyCardPurchaseSuccess</title>
<style>
.left {float:left;padding:0 10px 0 0; width:40px;}
.right {float:right;padding:0 0 0 10px;}
input {width:34px; text-align:center;}
#top {width:150px;}
</style>
<script>
function RndNum(){
var rnd="";
rnd+=Math.floor(Math.random()*10);
return Number(rnd)+Number(1);
}
var s = "";
var arr = ['+','-','*','/'];
var score = 0;
window.onload = function show(){
for(i = 0; i < 10 ; i++){
s += "<div><div class='left' id='question_" + i + "'></div><input value='' maxlength='4' onblur='onblurEvent(this.id)' type='text' id='" + i + "' /><div class='right' id='result_" + i + "'></div></div>";
}
s += "<div id='result'></div>";
document.getElementById("top").innerHTML = s;
showQuestion();
}
function showQuestion(){
for(i = 0; i < 10 ; i++){
var q = "";
q = RndNum() + arr[RndNum()%4] + RndNum();
document.getElementById("question_" + i).innerHTML = q;
}
}
function onblurEvent(id){
var c=0;
var getNum = document.getElementById(id).value;
if(checkAnswer(id,getNum)){
document.getElementById("result_"+id).innerHTML = "right";
}else{
document.getElementById("result_"+id).innerHTML = "wrong";
}
document.getElementById(id).disabled = true;
for(j = 0 ; j < 10 ; j++){
if(document.getElementById(j).disabled == true){
c++;
if(c == 10){
document.getElementById("score").innerHTML = "总分为 : " + score + " 分";
}
}
}
}
function checkAnswer(id,value){
var str = document.getElementById("question_"+id).innerHTML;
if(Number(eval(str)).toFixed(2) == Number(value)){
score += 10;
return true;
}else{
return false;
}
}
function reflesh(){
window.location.reload();
}
</script>
</head>
<body>
<div id="top">
</div>
<br />
<div id="score"></div>
<br />
<button onclick="reflesh()">刷新</button>
</body>
</html>
包括小数点只能输入4位,保留两位小数,输入框失去焦点就算出正确与否并锁定,全部锁定算出总分。
另外还是觉得你这么直接伸手要代码是不好的。正好我也在学习练练手,就发给你了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
自己编写呗~这样赤果果的伸手要程序啊~你自行编写,不会的在提问吧~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询