简体   繁体   English

计算小于或等于一个数字的正值之和

[英]Calculate the sum of positive values smaller or equal to a number

I am trying to calculate the sum of positive values smaller or equal to the entered number, for ex: 5 -> 1+2+3+4+5 = 15 我正在尝试计算小于或等于输入数字的正值之和,例如:5-> 1 + 2 + 3 + 4 + 5 = 15

I came up with this: 我想出了这个:

 var num = Number(prompt("Enter a number ")); sum = 0; i = num; do { sum = sum += i; i-- document.write(sum); } while (i > 0); 

I don't understand what I am doing wrong. 我不明白我在做什么错。

i think this is correct code: 我认为这是正确的代码:

var num = Number(prompt("Enter a number "));
sum = 0;
i = num;
do 
{
  sum += i;
  i--;
}
while (i > 0);
document.write(sum);

and i suggest you to use this formula : document.write((num * (num + 1)) / 2); 我建议您使用以下公式: document.write((num * (num + 1)) / 2);

If you look closer to your task, you'll find out, that: 如果您更接近自己的任务,就会发现:

If Num = 1, the sequence to be summed is [1] 如果Num = 1,则要求和的序列为[1]

if Num = 2, the sequence is [1, 2] 如果Num = 2,则序列为[1、2]

if Num = 3, the sequence is [1, 2, 3] 如果Num = 3,则序列为[1、2、3]

You can imagine, that you have a square with sides equal to num, for example, when num = 4: 您可以想象,例如,当num = 4时,您有一个边等于num的正方形:

****
****
****
****

And you need to summ 1, 2, 3, 4: 您需要对1、2、3、4求和:

***#
**##
*###
####

See? 看到? It's a square of a triangle. 这是一个三角形的正方形。

It could be calculated by formula: num * (num + 1) / 2 可以通过公式计算:num *(num + 1)/ 2

So, you code could be: 因此,您的代码可能是:

var num = Number(prompt("Enter a number "));
document.write(num * (num + 1) / 2)

You should write the answer at the end of loop and make this simple sum += i;. 您应该在循环末尾写下答案,并使这个简单的和+ = i;。

 var num = Number(prompt("Enter a number")); sum = 0; i = num; do { sum += i; i--; } while (i > 0); document.write(sum); 

You are writing the sum on each loop instead you have to print it finally. 您在每个循环上写和,而不是必须最终打印。 If you want to print the numbers then keep it an array and join them with + symbol before writing. 如果要打印数字,则将其保留为数组,并在写入前将它们与+符号连接起来。 To make it in ascending order change the loop condition. 要使其升序更改循环条件。

 var num = Number(prompt("Enter a number ")); sum = 0; i = 1; nums = []; do { sum = sum += i; nums.push(i++); } while (i <= num); document.write(nums.join(' + ') + ' = ' + sum); 

Do with increment instead of decrements.And also show result of sum outside of loop .Not with in loop.And create array to append increment value.Finally print with document.write 用增量而不是减量进行处理,并且还显示循环外求和的结果。不使用in循环。创建数组以附加增量值。最后用document.write打印

 var num=Number(prompt("Enter a number ")); sum = 0; i = 1; var a=[]; do { sum +=i; a.push(i) i++; } while (num >= i); document.write(a.join('+')+'='+sum) 

var number = 5, // Your number
    result = 0;

while ( number !== 0 ) {
    result += number;
    number--;
}

document.write(result);

Fast and precious solution. 快速而宝贵的解决方案。

Here is the case with complete check and display as you need: JAVA 在这种情况下,可以根据需要进行完整的检查和显示:JAVA

     public static void main ( String arg[]) {

    Scanner scan = new Scanner(System.in);
    int number = scan.nextInt();
    System.out.println("Number entered :  " + number);
    int sum =1 ;

    if(number > 1) {

        int nextNumber = 1;
        System.out.print(nextNumber);

        do {
            // sum of all the positive numbers 
            nextNumber++ ;
            sum = nextNumber + sum;
            System.out.print( " + " + nextNumber);

        }while(nextNumber < number);

        System.out.print(" = " + sum);
    }
}

 var num = Number(prompt("Enter a number")); sum = 0; for (i = num; i > 0; i--) { sum += i; } document.write(sum); 

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

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