java - 如何仅与一个起始整数平方?
问题描述
我是编程新手。我要编写一个使整数平方的代码。规则是:
- 输入仅包含一个整数
- 它最多只能循环5次
- 输出结果应为 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]);
}
}
}
解决方案
也许你正在寻找这样的东西。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;
}
}
}
推荐阅读
- android - 以编程方式将 TextView 样式设置为 TabLayout.Tab 默认样式
- spring - Spring Boot Mail 使用访问令牌发送电子邮件
- allure - Codeceptjs:Allure 报告未显示“测试正文”部分中的所有步骤
- php - 在 if 语句中返回带有 $errors 的重定向
- c# - 使用 UserControl 的 Page.xaml 中的 VisualStateManager 在 UWP 应用程序中不起作用
- java - 在 Pageable 中自定义 REST 参数的路径
- r - 数据框:将每个组除以对应于R中每个组的向量?
- python - 使用十六进制值 PYTHON 覆盖文件中的咬痕
- svn - 更改日志消息列表中的字体大小
- opencv - cv::Size 断言失败