如何固定在Java输入的偶数之和?

时间:2019-06-14 07:01:29

标签: javascript html

我只是使用C语言编写了这段代码并成功运行,但是我试图用Javascript编写它不起作用.......

谢谢!!!

function addNumbers(){
	    var a = Number(document.getElementById("num1").value);
		var b = Number(document.getElementById("num2").value);
		var c = Number(document.getElementById("num3").value);
		var d = Number(document.getElementById("num4").value);
		var e = Number(document.getElementById("num5").value);
		var sum = document.getElementById("answer").value;
		if (var a%2==0){
            var sum = var sum + var a;
        }

        if (var b%2==0){
            var sum = var sum + var b;
        }

        if (var c%2==0){
            var sum = var sum + var c;
        }

        if (var d%2==0){
            var sum = var sum + var d;
        }

        if (var e%2==0){
            var sum = var sum + var e;
        }
     }
    <table>
			<tr>
				<td>1</td>
				<td><input class="txt" id="num1">
			</tr>
			<tr>
				<td>2</td>
				<td><input class="txt" id="num2">
			</tr>
			<tr>
				<td>3</td>
				<td><input class="txt" id="num3">
			</tr>
			<tr>
				<td>4</td>
				<td><input class="txt" id="num4">
			</tr>
			<tr>
				<td>5</td>
				<td><input class="txt" id="num5">
			</tr>
			<tr>
				
				<td><input type="button" name="Sumbit" value="+" onclick="addNumbers()"/>
			</tr>
			<tr>
				<td>SUM:</td>
				<td><input class="txt" id="answer">
			</tr>
			</table>

html代码

5 个答案:

答案 0 :(得分:0)

不要在每个变量名之前添加“ var”,只需声明一次即可。

function addNumbers()
{
  var a = Number(document.getElementById("num1").value);
  var b = Number(document.getElementById("num2").value);
  var c = Number(document.getElementById("num3").value);
  var d = Number(document.getElementById("num4").value);
  var e = Number(document.getElementById("num5").value);
  var sum = document.getElementById("answer").value;
  if (a%2==0)
  {
    sum = sum + a;
  }
  if (b%2==0)
  {
    sum = sum + b;
  }
  if (c%2==0)
  {
    sum = sum + c;
  }
  if (d%2==0)
  {
    sum = sum + d;
  }
  if (e%2==0)
  {
    sum = sum + e;
  }
}

答案 1 :(得分:0)

尝试不再声明var。 只是添加它,不要这样做

 if (var d%2==0){
     var sum = var sum + var d;
  }

这样做

 if (var d%2==0){
     sum += d;
  }

针对每种情况

答案 2 :(得分:0)

您可以通过将输入的id直接用作具有value属性的变量来收集数组中的所有值,并通过检查并添加值来减少此值。

function calc() {
    sum.innerHTML = [num1.value, num2.value, num3.value, num4.value, num5.value]
        .reduce((s, v) => s + (v % 2 === 0 && +v), 0);
}
<input id="num1" type="text"><br>
<input id="num2" type="text"><br>
<input id="num3" type="text"><br>
<input id="num4" type="text"><br>
<input id="num5" type="text"><br>
<button onclick="calc();">calc</button><br>
Even Sum: <span id="sum">0</span>

答案 3 :(得分:0)

您的代码中有很多错误,但是我在下面提供了功能实现的摘要:

//Create HTML Elements
for (var i = 1; i <= 5; i++) {
    var label = document.body.appendChild(document.createElement("label"));
    label.innerHTML = "Number " + i + ": ";
    var input = label.appendChild(document.createElement("input"));
    input.type = "number";
    input.id = "num" + i;
    document.body.appendChild(document.createElement("br"));
}
var resultField = document.body.appendChild(document.createElement("input"));
resultField.id = "answer";
//Sum function
function addNumbers() {
    var a = parseFloat(document.getElementById("num1").value) || 0;
    var b = parseFloat(document.getElementById("num2").value) || 0;
    var c = parseFloat(document.getElementById("num3").value) || 0;
    var d = parseFloat(document.getElementById("num4").value) || 0;
    var e = parseFloat(document.getElementById("num5").value) || 0;
    var sum = 0;
    if (a % 2 == 0) {
        sum = sum + a;
    }
    if (b % 2 == 0) {
        sum = sum + b;
    }
    if (c % 2 == 0) {
        sum = sum + c;
    }
    if (d % 2 == 0) {
        sum = sum + d;
    }
    if (e % 2 == 0) {
        sum = sum + e;
    }
    document.getElementById("answer").value = sum;
}
//Create button and bind events
var btn = document.body.appendChild(document.createElement("button"));
btn.innerHTML = "Calculate";
btn.addEventListener("click", addNumbers);

要考虑的错误:

  1. 请勿在每次使用var时重新声明变量。
  2. 从JavaScript中的用户输入中获取数字时,请准备处理NaN值。
  3. 变量不是双向绑定。计算完成后,将sum声明为数值0,并将该值发送到html元素。

答案 4 :(得分:0)

这是您的完整代码,现在可以使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <td>1</td>
        <td><input class="txt" id="num1">
    </tr>
    <tr>
        <td>2</td>
        <td><input class="txt" id="num2">
    </tr>
    <tr>
        <td>3</td>
        <td><input class="txt" id="num3">
    </tr>
    <tr>
        <td>4</td>
        <td><input class="txt" id="num4">
    </tr>
    <tr>
        <td>5</td>
        <td><input class="txt" id="num5">
    </tr>
    <tr>
        <td><input name="Sumbit" onclick="addNumbers()" type="button" value="+"/>
    </tr>
    <tr>
        <td>SUM:</td>
        <td><input class="txt" id="answer">
    </tr>
</table>
</body>

<script>
    function addNumbers() {
        console.log("avc")
        var a = Number(document.getElementById("num1").value);
        var b = Number(document.getElementById("num2").value);
        var c = Number(document.getElementById("num3").value);
        var d = Number(document.getElementById("num4").value);
        var e = Number(document.getElementById("num5").value);
        var sum = 0;
        console.log(typeof a)
        if (a%2==0)
             sum +=  a;
        if ( b%2==0)
            sum +=  b;
        if ( c%2==0)
            sum +=  c;
        if ( d%2==0)
            sum +=  d;
        if ( e%2==0)
            sum +=  e;
        document.getElementById("answer").value = sum;
    }
</script>

</html>