Java for 循环生成“1,4,9,16,25,36,49,64,81,100”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22937436/
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 19:25:06  来源:igfitidea点击:

for loop to generate "1,4,9,16,25,36,49,64,81,100"

javaloopsfor-loop

提问by user3452963

how can I write a for loop to give this output? I was thinking a nested loop?

我怎样才能写一个 for 循环来给出这个输出?我在想一个嵌套循环?

for (int i = 0; i < 100; i++){
    for (int j = 0; j < i; j++) {

but i don't know how to go from there?

但我不知道从那里怎么走?

thanks

谢谢

采纳答案by NeoP5

try this... ;)

尝试这个... ;)

for (int i = 1; i <= 10; i++){
   int result = i * i;
   System.out.printLn(result);
}

回答by anirudh

I won't give out the answer but I'll give you a hint. That is a list of the first 10 perfect squares. So you just need one loop to go through 10 values and get their square.

我不会给出答案,但我会给你一个提示。这是前 10 个完全平方的列表。所以你只需要一个循环来遍历 10 个值并得到它们的平方。

回答by user6044559

for loop to generate “1,4,9,16,25,36,49,64,81,100”

for循环生成 “1,4,9,16,25,36,49,64,81,100”

class series
{
     public static void main(String[]args)
     {
        int i,j;
        for(i=1; i<=10; i++) {
             j=i*i;
             System.out.println(j);
        }
     }
}

回答by Lena

You have to iterate the numbers, and then append the list:

您必须迭代数字,然后附加列表:

for (int i =1; i <= 10; i++) {    
    System.out.print(i*i + " ");
}