我在不使用eval()功能的情况下制作javascript 计算器程序。我遇到的问题是我从按钮点击菜单让我们说7.我将它保存在num1
中,稍后我清除了输入屏幕,我再次想要输入并将其保存在num2
中操作我想向用户显示结果,但我无法保存第二个数字并显示它我不在问题所在。我从早上开始挣扎,感谢任何帮助,非常感谢。
var num1 = 0;
var num2 = 0;
var check = true;
var flag = true;
var memory = 0;
function getValue(par) {
document.getElementById("input").value = par.toString();
num1 = par;
if (flag == true) {
num1 = parseInt(par);
flag = false;
} else {
num2 = parseInt(par);
flag = true;
}
if (par == "C") {
document.getElementById("input").value = "";
num1 = 0;
num2 = 0;
}
}
function getOperator(val) {
document.getElementById("input").value = "";
if (val == "topla") {
memory = num1 + num2;
} else if (value = "=") {
document.getElementById("input").value = memory;
}
}
#wrapper {
width: 280px;
height: 350px;
border: 5px solid gray;
margin: 0 auto;
background-color: gray;
border-radius: 10px;
}
#numbers {
width: 210px;
height: 310px;
float: left;
}
#numbers button {
float: left;
border-radius: 15px;
font-size: 25px;
width: 60px;
height: 60px;
margin: 5px 5px;
border-radius: 5px;
color: white;
background-color: black;
}
#operators {
margin-top: 1px;
float: right;
width: 70px;
height: 310px;
}
#operators button {
display: block;
border-radius: 15px;
width: 60px;
height: 60px;
margin-top: 8px;
margin-left: 2px;
border-radius: 5px;
color: white;
background-color: red;
font-size: 25px;
}
#input {
margin-top: 5px;
border: 5px solid gray;
margin-left: 5px;
width: 270px;
height: 50px;
}
<div id="wrapper">
<div class="input1">
<input id="input">
</div>
<div id="numbers">
<button type="button" value="9" onclick="getValue('9')">9</button>
<button type="button" value="8" onclick="getValue('8')">8</button>
<button type="button" value="7" onclick="getValue('7')">7</button>
<button type="button" value="6" onclick="getValue('6')">6</button>
<button type="button" value="5" onclick="getValue('5')">5</button>
<button type="button" value="4" onclick="getValue('4')">4</button>
<button type="button" value="3" onclick="getValue('3')">3</button>
<button type="button" value="2" onclick="getValue('2')">2</button>
<button type="button" value="1" onclick="getValue('1')">1</button>
<button type="button" value="0" onclick="getValue('0')">0</button>
<button type="button" value="=" onclick="getOperator('=')">=</button>
<button type="button" value="C" onclick="getV*emphasized text*alue('C')">C</button>
</div>
<div id="operators">
<button type="button" value="topla" onclick="getOperator('topla')">+</button>
<button type="button" value="cıkar" onclick="getOperator('cıkar')">-</button>
<button type="button" value="bol" onclick="getOperator('carp')">/</button>
<button type="button" value="carp" onclick="getOperator('bol')">*</button>
</div>
</div>
答案 0 :(得分:1)
1. value
像这样添加加法。这只添加两个数字。
val
而不是if(val == "="){
因此,请更改if(value ="="){
而不是var num1 = 0;
var num2 = 0;
var check = true;
var flag = true;
var memory = 0;
function getValue(par) {
document.getElementById("input").value=par.toString();
num1=par;
if(flag) {
num1=parseInt(par);
flag=false;
}else{
num2=parseInt(par);
console.log(num2)
flag=false;
}
if(par =="C"){
document.getElementById("input").value="";
num1=0;
num2=0;
}
}
function getOperator(val){
document.getElementById("input").value="";
if(val =="topla"){
memory= parseInt(num1 )+parseInt(num2);
}else if(val =="="){
document.getElementById("input").value=memory;
}
}
#wrapper {
width:280px;
height:350px;
border:5px solid gray;
margin: 0 auto;
background-color:gray;
border-radius: 10px;
}
#numbers{
width:210px;
height:310px;
float:left;
}
#numbers button {
float:left;
border-radius: 15px;
font-size: 25px;
width:60px;
height:60px;
margin:5px 5px;
border-radius: 5px;
color:white;
background-color:black;
}
#operators {
margin-top:1px;
float:right;
width:70px;
height:310px;
}
#operators button{
display:block;
border-radius: 15px;
width:60px;
height:60px;
margin-top:8px;
margin-left:2px;
border-radius: 5px;
color:white;
background-color:red;
font-size: 25px;
}
#input {
margin-top:5px;
border:5px solid gray;
margin-left:5px;
width:270px;
height:50px;
}
&#13;
<html>
<head>
<meta charset="utf-8">
<title>My Calculator</title>
<link href="calculator.css" type="text/css" rel="stylesheet"/>
<script src="calculator.js" type="text/javascript"></script>
</head>
<div id="wrapper">
<div class="input1">
<input id="input" >
</div>
<div id="numbers">
<button type="button" value="9" onclick="getValue('9')">9</button>
<button type="button" value="8" onclick="getValue('8')" >8</button>
<button type="button" value="7" onclick="getValue('7')">7</button>
<button type="button" value="6" onclick="getValue('6')">6</button>
<button type="button" value="5" onclick="getValue('5')">5</button>
<button type="button" value="4" onclick="getValue('4')">4</button>
<button type="button" value="3" onclick="getValue('3')">3</button>
<button type="button" value="2" onclick="getValue('2')">2</button>
<button type="button" value="1" onclick="getValue('1')">1</button>
<button type="button" value="0" onclick="getValue('0')">0</button>
<button type="button" value="=" onclick="getOperator('=')">=</button>
<button type="button" value="C" onclick="getValue('C')">C</button>
</div>
<div id="operators">
<button type="button" value="topla" onclick="getOperator('topla')" >+</button>
<button type="button" value="cıkar" onclick="getOperator('cıkar')" >-</button>
<button type="button" value="bol" onclick="getOperator('carp')">/</button>
<button type="button" value="carp" onclick="getOperator('bol')">*</button>
</div>
</div>
</html>
&#13;
public void cameraIntent(Context context)
{
Intent takingPictureCameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
((AppCompatActivity)context).startActivityForResult(takingPictureCameraintent, REQUEST_CAMERA);
}
}
&#13;
答案 1 :(得分:0)
主要问题是,当您按下+
按钮时,您还不知道第二个号码是什么,因为用户仍然需要输入该号码。所以这样做:
if(val=="topla"){
memory=num1+num2;
}
......为时过早:num2
尚未知晓(0)。
相反,您应该将选定的运算符保存在变量中,然后当用户按下=
时,将同时包含数字和运算符。那时你可以进行评估。
因此将其用作 getOperator
var oper = ''; // new variable
function getOperator(val){
document.getElementById("input").value="";
if(val=="="){
if(oper=="topla"){
memory=num1+num2;
}
// add other operators here ...
//
document.getElementById("input").value=memory;
} else {
oper = val; // save operator for later use...
}
}
请注意,您仍然面临许多挑战:
这当然超出了这个问题的范围。