简体   繁体   English

如何使用 while-loop、do-loop 从 1 到用户输入的数字求和?

[英]How to sum numbers from 1 through user's entry, using while-loop, do-loop?

I am trying to complete the following code to achieve the sum of numbers from 1 to 100(user's entry must be 1-100), using whileloop or doloop.我正在尝试使用 whileloop 或 doloop 完成以下代码以实现从 1 到 100 的数字总和(用户的输入必须是 1-100)。 I am new to this so, any help is much appreciated!我是新手,非常感谢任何帮助!

In the following code, I used prompt method to get the user entry.在下面的代码中,我使用提示方法来获取用户条目。 Wrote the code, to sum numbers;编写代码,对数字求和; from 1 through the user's entry.从 1 到用户的输入。 I displayed the result in an alert box.我在警告框中显示了结果。 Now, my challenge is I want to display an error message if the user's entry outside the 1-100 range.现在,我的挑战是如果用户的输入超出 1-100 范围,我想显示一条错误消息。 And after that, I do not want to do any calculations if user clicks cancel and stop displaying the prompt box.之后,如果用户单击取消并停止显示提示框,我不想进行任何计算。

<!DOCTYPE html>
        <html>
        <head>
        <title>Sum of Numbers</title>
     <script>
      var numbers = prompt("Enter a  number 1-100");
    
      while (numbers!=null && (isNaN(parseInt(numbers)) || parseInt(numbers) >100 || parseInt(numbers) <1)) {
          numbers = prompt("Try again.Enter a  number 1-100");
      }
      if (numbers !=null){
            alert("Finally you entered a correct number");
      }
     
      var sum = 0;
      var numOfLoops = numbers;
      var counter = 1;
      do {
        sum+=counter;
        counter++;
      } while (counter<=numOfLoops)
      alert ("sum=" +sum);
    
     </script>
    
    </head>
    <body>
        <script>
        document.write("<h1>Sum of Numbers</h1>");
            document.write("The sum of numbers from 1 to = " + numbers +  "&nbsp;is = "  +
                + sum + "<br><br>");
        </script>
    
    </body>
    </html>

Simply move the calculation logic inside the condition where the user enters the correct input.只需在用户输入正确输入的条件内移动计算逻辑即可。 This will make sure that the prompt closes automatically when you click on the cancel button (Prompt returns null when the user clicks on cancel)这将确保当您单击取消按钮时提示会自动关闭(当用户单击取消时,提示返回null

    <script>
        var numbers = prompt("Enter a  number 1-100");

        while (numbers != null && (isNaN(parseInt(numbers)) || parseInt(numbers) > 100 || parseInt(numbers) < 1)) {
            numbers = prompt("Try again.Enter a  number 1-100");
        }
        if (numbers != null) {
            alert("Finally you entered a correct number");

            var sum = 0;
            var numOfLoops = numbers;
            var counter = 1;
            do {
                sum += counter;
                counter++;
            } while (counter <= numOfLoops)
            alert("sum=" + sum);
        }

    </script>

You could simply use a do…while to solve your problem, eg :您可以简单地使用do…while来解决您的问题,例如

 let n = null; do { n = parseInt(prompt('Enter an int number between 1 and 100')); } while (isNaN(n) || (n < 1 || n > 100)); let sum = n * (n + 1) / 2; alert('The sum of all int numbers from 1 to ' + n + ' is: ' + sum);

NB The sum of the first n integer numbers can be computed as n * ( n + 1) / 2, with O (1) complexity - reducing the O ( n ) complexity of your for loop.注意n 个integer 数字的总和可以计算为n * ( n + 1) / 2,复杂度为O (1) - 降低了for循环的O ( n ) 复杂度。

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

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