Java怎么用js编写计算器
1个回答
2018-01-02 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
核心代码:
<script>
var initData = "";
var lastop = "";
var delop = "";
var result = 0;
var num1 = 0;
var num2 = 0;
function getNum(num){
initData += num;
showData(initData);
}
function showData(data){
document.getElementById("result").value = data;
}
function getOp(op){
lastop = op;
num1 = parseFloat(initData);
initData = "";
}
function getResult(){
num2 = parseFloat(initData);
initData = "";
result = calc(num1,num2,lastop);
showData(result);
}
function calc(num1,num2,lastop){
switch(lastop){
case '+':result = num1 + num2;break;
case '-':result = num1 - num2;break;
case '*':result = num1 * num2;break;
case '/':if(num2 == 0)return ;result = num1 / num2;break;
case '%':if(num2 == 0)return ;result = num1 % num2;break;
}
return result;
}
function deleteNum(delop){
if(delop=='C'){
initData = "";
num1 = 0;
num2 = 0;
lastop = "";
showData("0.");
}
if(delop=='CE'){
initData = initData.substring(0,initData.length-1);
showData(initData);
}
}
</script>
<script>
var initData = "";
var lastop = "";
var delop = "";
var result = 0;
var num1 = 0;
var num2 = 0;
function getNum(num){
initData += num;
showData(initData);
}
function showData(data){
document.getElementById("result").value = data;
}
function getOp(op){
lastop = op;
num1 = parseFloat(initData);
initData = "";
}
function getResult(){
num2 = parseFloat(initData);
initData = "";
result = calc(num1,num2,lastop);
showData(result);
}
function calc(num1,num2,lastop){
switch(lastop){
case '+':result = num1 + num2;break;
case '-':result = num1 - num2;break;
case '*':result = num1 * num2;break;
case '/':if(num2 == 0)return ;result = num1 / num2;break;
case '%':if(num2 == 0)return ;result = num1 % num2;break;
}
return result;
}
function deleteNum(delop){
if(delop=='C'){
initData = "";
num1 = 0;
num2 = 0;
lastop = "";
showData("0.");
}
if(delop=='CE'){
initData = initData.substring(0,initData.length-1);
showData(initData);
}
}
</script>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询