我有一个任务,我必须使用 java 脚本和 HTML 创建一个计算器。我已经编写了足够多的代码,当我使用运算符时,它会进行数学运算,但数字不会显示在显示框中。我也不确定如何编写清除按钮和否定按钮。这是外部 javascript 文件。
var mycalc = new calc(null,null,null);
function calc(firstNum,secNum,operator)
{
this.firstNum = firstNum;
this.secNum = secNum;
this.operator = operator;
}
function numberClick(ref)
{
//alert.ref.value
if(mycalc.firstNum == null)
mycalc.firstNum = ref.value
else if (mycalc.secNum == null)
mycalc.secNum = ref.value //ref.value is the value from the input
//alert("firstNum" + mycalc.operator.firstNum);
//alert("secNum") + mycalc.secNum);
}
function operatorClick(ref)
{
if (mycalc.operator == null)
mycalc.operator = ref.value;
}
function calculateResultClick()
{
if (mycalc.operator == "+")
{
var ans = parseInt(mycalc.firstNum) + parseInt(mycalc.secNum);
alert(ans);
}
if (mycalc.operator == "-")
{
var ans2 = parseInt(mycalc.firstNum) - parseInt(mycalc.secNum);
alert(ans2);
}
if (mycalc.operator == "*")
{
var ans3 = parseInt(mycalc.firstNum) * parseInt(mycalc.secNum);
alert(ans3)
}
if (mycalc.operator == "/")
{
var ans4 = parseInt(mycalc.firstNum) / parseInt(mycalc.secNum);
alert(ans4)
}
}
function Clear()
{
}