我得到了一个使用HTML和JavaScript创建简单计算器的任务。 我已经构建了一个简单的HTML页面,其中包含三个输入,用于两个数字和一个运算符 我还创建了一个带有将运行计算器的函数的JS页面。
当我使用HTML页面中的<script>
标记连接这两个页面时,当我检查JS页面时会弹出一条简短的消息:&#34;找不到CalculatorJS的定义。 JS&#34; (我的JS页面)。
这是我的HTML页面代码:
<head>
<title>Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<h3>Enter two numbers and an operation in the right places below</h3>
<br>
<br>
<span><span> First Number</span> <span>Operation</span> <spaqn>Second Number</span> </span>
<br>
<input type="text" id="firstNum">
<span></span>
<input type="text" id="operation">
<span></span>
<input type="text" id="secondNum">
<span></span>
<script src="CalculatorJS.js"></script>
<button onclick="Calculate()">Calculate</button>
</body>
这是我的JS页面代码:
var num1 = document.getElementById("firstNum").value;
var num2 = document.getElementById("secondNum").value;
var operator = document.getElementById("operation").value;
var resulte = 0;
function Calculate(num1, operator,num2)
{
if(operator == "+")
{
resulte = num1 + num2;
}
else if(operator == '-')
{
resulte = num1 - num2;
}
else if(operator == '*')
{
resulte = num1 * num2;
}
else if(operator == '/')
{
reulte = num1 / num2;
}
else{
resulte = 'InValid Operator';
}
alert(resulte);
}
function Calculate2(num1, operator,num2)
{
switch (operator) {
case value: '+'
return num1 + num2;
break;
case value: '-'
return num1 - num2;
break;
case value: '*'
return num1 * num2;
break;
case value: '/'
return num1 / num2;
break;
default:
return alert("InValid Charachter")
break;
}
}
我在其中任何一个页面上都做了一些让计算器不起作用的东西吗? 当我尝试进行像1 + 1这样的简单计算时, 我得到了我写过的警告&#34; InValid Operator&#34;。
答案 0 :(得分:0)
In [22]: df[['protein']].join(df[df.columns[df.eq('C').any()]])
Out[22]:
protein 1 2 4
0 prot1 C M F
1 prot2 C D M
2 prot3 C C F
3 prot4 S D C
4 prot5 S D I
答案 1 :(得分:0)
加载页面时,变量num1
,num2
和operation
取空值。之后,当您输入值并触发Calculate
函数时,这三个变量什么也得不到,因为它们不在Calculate
方法之内,因此它显示“无效的运算符”。
您必须在num1
函数中声明num2
,operation
和Calculate
。然后这三个变量将得到它们的值并显示结果。检查以下内容:
var resulte = 0;
function Calculate()
{
var num1 = parseInt(document.getElementById("firstNum").value);
var num2 = parseInt(document.getElementById("secondNum").value);
var operator = document.getElementById("operation").value;
if(operator == "+")
{
resulte = num1 + num2;
}
else if(operator == '-')
{
resulte = num1 - num2;
}
else if(operator == '*')
{
resulte = num1 * num2;
}
else if(operator == '/')
{
resulte = num1 / num2;
}
else{
resulte = 'InValid Operator';
}
alert(resulte);
}
function Calculate2(num1, operator,num2)
{
switch (operator) {
case value: '+'
return num1 + num2;
break;
case value: '-'
return num1 - num2;
break;
case value: '*'
return num1 * num2;
break;
case value: '/'
return num1 / num2;
break;
default:
return alert("InValid Charachter")
break;
}
}
<head>
<title>Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<h3>Enter two numbers and an operation in the right places below</h3>
<br>
<br>
<span><span> First Number</span> <span>Operation</span> <span>Second Number</span> </span>
<br>
<input type="text" id="firstNum">
<span></span>
<input type="text" id="operation">
<span></span>
<input type="text" id="secondNum">
<span></span>
<button onclick="Calculate()">Calculate</button>
</body>
答案 2 :(得分:-1)
<head>
<title>Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<h3>Enter two numbers and an operation in the right places below</h3>
<br>
<br>
<span><span> First Number</span> <span>Operation</span>
<spaqn>Second Number</span>
</span>
<br>
<input type="text" id="firstNum">
<span></span>
<input type="text" id="operation">
<span></span>
<input type="text" id="secondNum">
<span></span>
<button onclick="Calculate()">Calculate</button>
</body>
<script>
var resulte = 0;
function Calculate() {
var num1 = parseInt(document.getElementById("firstNum").value);
var num2 = parseInt(document.getElementById("secondNum").value);
var operator = document.getElementById("operation").value;
if (operator == "+") {
resulte = num1 + num2;
} else if (operator == '-') {
resulte = num1 - num2;
} else if (operator == '*') {
resulte = num1 * num2;
} else if (operator == '/') {
resulte = num1 / num2;
} else {
resulte = 'InValid Operator';
}
alert(resulte);
}
</script>
&#13;
答案 3 :(得分:-1)
<head>
<title>Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<h3>Enter two numbers and an operation in the right places below</h3>
<br>
<br>
<span><span> First Number</span> <span>Operation</span>
<spaqn>Second Number</span>
</span>
<br>
<input type="text" id="firstNum">
<span></span>
<input type="text" id="operation">
<span></span>
<input type="text" id="secondNum">
<span></span>
<button onclick="Calculate()">Calculate</button>
<script>
var resulte = 0;
function Calculate() {
var num1 = document.getElementById("firstNum").value;
var num2 = document.getElementById("secondNum").value;
var operator = document.getElementById("operation").value;
if (operator == "+") {
resulte = Number(num1) + Number(num2);
} else if (operator == '-') {
resulte = Number(num1) - Number(num2);
} else if (operator == '*') {
resulte = Number(num1) * Number(num2);
} else if (operator == '/') {
if (Number(num2) == 0) {
resulte = 'Invalid Operation';
alert('Division by zero is undefined.')
} else {
resulte = Number(num1) / Number(num2);
}
} else {
resulte = 'InValid Operator';
}
alert(resulte);
}
</script>
</body>