首页 > 解决方案 > 如何仅与一个起始整数平方?

问题描述

我是编程新手。我要编写一个使整数平方的代码。规则是:

  1. 输入仅包含一个整数
  2. 它最多只能循环5次
  3. 输出结果应为 3 个整数位

以下是我想要的结果:

输入:

1

输出:

001
004
009
016
025

下面是我尝试的代码,但它不起作用。

int start=input.nextInt();

for(int i=5; i <=5; i++; start++){
    num[i]= start*start;
    }
    for (i=0; i<num.length; i++){
        System.out.println(“%03d\n”, num[i]);
    }
    }
    }

标签: javaarrays

解决方案


也许你正在寻找这样的东西。Ideone 链接https://www.ideone.com/IzO18y

import java.util.*;
import java.lang.*;
import java.io.*;
 
 
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
 
        for(int i=1;i<=5;i++){
            System.out.printf("%03d\n",n*n);
            n+=1;
        }
    }
}

推荐阅读