首页 > 解决方案 > 如何创建一个以两个数字作为参数(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;
  }
 }

标签: javaarraysnumbers

解决方案


你右边的值没有改变,所以试试像

list[index] = ((index + 1) * num);

2不需要乘以。同样根据@Andreas评论,直接使用参数更清楚


推荐阅读