java - 如何创建一个以两个数字作为参数(num,length)并返回一个由 num 的倍数组成的数组的函数?
问题描述
我需要创建一个函数,该函数将两个数字作为参数(num,length)并返回一个由 num 的倍数组成的数组,直到长度。到目前为止,我有这个:
import java.util.*;
public class Program {
public static int[] arrayOfMultiples(int num, int length) {
final int LIMIT = length, MULTIPLE = num;
int[] list = new int[LIMIT];
for (int index = 0; index < LIMIT; index++)
list[index] = (MULTIPLE *2);
return list;
}
}
解决方案
你右边的值没有改变,所以试试像
list[index] = ((index + 1) * num);
2
不需要乘以。同样根据@Andreas评论,直接使用参数更清楚
推荐阅读
- r - 基于 If Statments R 创建一个新列
- c - ttyUSB 将接收到的数据写回
- android - 使用 FCM 注销后重新登录时未收到推送通知
- wso2 - WSO2 API 管理器。授权码授予 PKCE
- wordpress - 如何为 wordpress 子域(具有单个站点的子域)制作单独的主页
- laravel - Laravel 雄辩的一对多关系数据未显示
- python - 当文件包含 if __name__ == '__main__' 时,python manage.py shell < scripts/myscript.py 不起作用
- matplotlib - 如何修复此直方图,使条形图等距且标签位于条形图的中心。?
- hive - 如何在 hive stack 命令中设置十进制值
- jquery - 如何为布局页面和内容页面定义 Jquery 脚本