简体   繁体   English

如何在输入零之前继续接收数字并计算用户输入的正数和负数总数。在文本字段中

[英]How to continue receiving numbers until a zero is entered and count total positive and negative numbers the user entered.in the textfield

More Details:更多细节:

Program should receive a number and continue receiving numbers [from users input] until a zero is entered.程序应该接收一个数字并继续接收数字[来自用户输入],直到输入一个零。 When a zero is entered, the program should output how many positive and how many negative numbers have been entered, and then stop.当输入一个零时,程序应该 output 已经输入了多少个正数和多少个负数,然后停止。

So far I've only been able to enter one number at a time in the textfield and only been able to output one value that is either positive or negative.到目前为止,我一次只能在文本字段中输入一个数字,并且只能 output 一个正值或负值。

HTML HTML

<!DOCTYPE html>
<html>
<head>
    <title>Exercise Two</title>
    <link href="ex2.css" rel="stylesheet">
    <script type="text/javascript" src="ex2.js"></script>
</head>
<body>

Enter number (Press 0 to stop)
<input type="text" id="num">
<button onclick="check()">Submit</button>

</body>
</html>

Javascript Javascript

function check(){

    let btnClear = document.querySelector('button');
    let inputs = document.querySelectorAll('input');
 
    btnClear.addEventListener('click', () => {
        inputs.forEach(input =>  input.value = '');
});

    var x = parseInt(document.getElementById("num").value);
    var posCount = 0;
    var negCount = 0;

    if (x > 0) {
        posCount++;
    }else{
        negCount++;
    }
    

    alert("Positive numbers: " + posCount + "\nNegative numbers: " + negCount);
}

You could try appending the data to a hidden input element:您可以尝试将数据附加到隐藏的input元素:

 function check() { let btnClear = document.querySelector('button'); let inputs = document.querySelectorAll('input'); btnClear.addEventListener('click', () => { inputs.forEach(input => input.value = ''); }); var x = parseInt(document.getElementById("num").value); var totals = document.querySelector("#totals"); var posCount = totals.dataset.positives; var negCount = totals.dataset.negatives; if (x === 0) { alert("Positive numbers: " + posCount + "\nNegative numbers: " + negCount); totals.dataset.positives = 0; totals.dataset.negatives= 0; } else if (x > 0) { totals.dataset.positives = ++posCount; } else { totals.dataset.negatives= ++negCount; } }
 <p>Enter number (Press 0 to stop)</p> <input type="text" id="num"> <input type="hidden" id="totals" data-positives='0' data-negatives='0'> <button onclick="check()">Submit</button>

UPDATE更新

To achieve this, you can create a positive and negative variable outside of the count function and then check each input value;为此, you can create a positive and negative variable outside ,然后检查每个输入值; if the value entered zero, you can call your alert function and print the result.如果输入的值为零,您可以调用警报 function 并打印结果。

Here is the working code:这是工作代码:

 var posCount = 0; var negCount = 0; function printOutput () { alert("Positive numbers: " + posCount + "\nNegative numbers: " + negCount); } function check(){ let btnClear = document.querySelector('button'); let inputs = document.querySelectorAll('input'); var x = parseInt(document.getElementById("num").value); document.getElementById("num").value = '' if (x > 0) { posCount++; }else if (x < 0){ negCount++; } else { printOutput(); // re-initiate value posCount = 0; negCount = 0; } }
 <.DOCTYPE html> <html> <head> <title>Exercise Two</title> <link href="ex2.css" rel="stylesheet"> <script type="text/javascript" src="ex2.js"></script> </head> <body> Enter number (Press 0 to stop) <input type="text" id="num"> <button id="submit-btn" onclick="check()">Submit</button> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM