Java 1 到 100 之间所有平方的总和?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22849505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:22:43  来源:igfitidea点击:

The sum of all squares between 1 and 100 inclusive?

java

提问by nickmcneely7

Doing an assignment for my computer science class and can't quite figure out what I'm doing wrong. I have to write a program that adds all the squares between 1 and 100 (1, 4, 9, 16, 25, 36, 49, 64, 81, 100) As part of the assignment I'm required to use a "while" statement, so no "for" statements will help. My current code is as follows.

为我的计算机科学课做作业,但无法弄清楚我做错了什么。我必须编写一个程序,将 1 和 100(1、4、9、16、25、36、49、64、81、100)之间的所有平方相加作为作业的一部分,我需要使用“while " 语句,因此没有“for”语句会有所帮助。我目前的代码如下。

import java.util.Scanner;

public class While42B {
    public static void main(String []args ) {
        Scanner reader = new Scanner(System.in);
        int n = 1;
        int sum = 0;
        while (n <= 100) {
            n = (n*n);
            n++;

            sum = (sum + n); 
        }
        System.out.println(sum);
    }
}

And the return I get from the GUI is

我从 GUI 得到的回报是

710

710

Any help would be greatly appreciated, thank you!

任何帮助将不胜感激,谢谢!

采纳答案by Ashraful Haque

If you want to do it in your way I mean first square the value of each number,keep it in a variable and add it to the sum you can use a different variableinstead of nto store the square value of each number like this :

如果您想以自己的方式进行操作,我的意思是首先对每个数字的值进行平方,将其保存在一个变量中并将其添加到总和中,您可以使用 adifferent variable而不是n来存储每个数字的平方值,如下所示:

int n = 1;
int squareValue;
int sum = 0;
while (n <= 10) {

    squareValue= (n*n);

    sum += squareValue; 

    n++;
}
System.out.println(sum);

回答by nanofarad

Look at this statement:

看看这个声明:

n = (n*n);

You're squaring ninside the loop, and then incrementing it. Do the following instead:

你在n循环内平方,然后增加它。请改为执行以下操作:

while (n <= 10) {
    sum = (sum + (n*n)); 
    n++;
}

This way, you don't modify nby squaring it, and you can properly track its value for the while loop.

这样,您就不会n通过对它进行平方来修改它,并且您可以在 while 循环中正确跟踪它的值。

回答by CMPS

You are changing the value of n in: n= n*n So now you are not looping from 1 to 100, you are skipping a lot of numbers

您正在更改 n 的值: n= n*n 所以现在您不是从 1 循环到 100,而是跳过了很多数字

回答by Aman Agnihotri

Just as an add-on, in Java 8, one can do the sum of squares of first 10 natural numbers as follows:

作为一个附加组件,在 Java 8 中,可以按如下方式计算前 10 个自然数的平方和:

int sum = IntStream.rangeClosed(1, 10).map(n -> n * n).sum();

回答by peter.petrov

I realize you're looking for a while loop but
just FYI you can use the direct formula:

我意识到您正在寻找一个 while 循环,但
仅供参考,您可以使用直接公式:

System.out.println( n * (n + 1) * (2 * n + 1) / 6);

System.out.println( n * (n + 1) * (2 * n + 1) / 6);

回答by Nicholas John Grabill

Simply read the mathematics and then proceed to translate the mathematical operations into code. I've worked out the mathematics which I hope could help you. I have also provided an answer to the question stated by your professor, I do hope that I am able to have been helpful in your arrangements. No credit needed except to Carl Friedrich Gauss.

只需阅读数学,然后继续将数学运算转换为代码。我已经计算出我希望可以帮助你的数学。我也对你教授的问题进行了解答,希望对你的安排有所帮助。除了卡尔·弗里德里希·高斯 (Carl Friedrich Gauss) 之外,不需要任何功劳。

X(nsquared base 1 + nsquared base n) divided by 2

X(nsquared base 1 + nsquared base n) 除以 2

X equals number of numbers (100) and n base 1 equals first number (1) and n base n equals last number (100), I did not include the squares in the numerical description but you do need too include squares in the first and last number. 1 squared and 100 squared.

X 等于数字的数量 (100) 和 n 基数 1 等于第一个数字 (1) 和 n 基数 n 等于最后一个数字 (100),我没有在数字描述中包括正方形,但您也需要在第一个和最后一个号码。1 平方和 100 平方。

  • Do these operations according too PEMDAS ( order of operations ).
  • I don't have a high enough reputation so I can't post photos, sorry for that, I hope this all helped though.
  • The answer is (500,050)
  • 也按照 PEMDAS(操作顺序)执行这些操作。
  • 我没有足够高的声誉,所以我不能发布照片,对不起,我希望这一切都有帮助。
  • 答案是 (500,050)