Java 制作一个数组,打印给定数字的倍数

Java 制作一个数组,打印给定数字的倍数,java,Java,我需要帮助制作一个数组,打印出给定数字的倍数。输出如下所示: Enter a positive integer: 8 Test array: 1 1 2 3 5 8 13 Counting up: 1 2 3 4 5 6 7 8 Counting down: 8 7 6 5 4 3 2 1 The first 8 multiples of 5: 5 10 15 20 25 30 35 40 The first 8 multiples of 10: 10 20 30 40 50 60 70 80

我需要帮助制作一个数组,打印出给定数字的倍数。输出如下所示:

Enter a positive integer: 8
Test array: 1 1 2 3 5 8 13
Counting up: 1 2 3 4 5 6 7 8
Counting down: 8 7 6 5 4 3 2 1
The first 8 multiples of 5: 5 10 15 20 25 30 35 40
The first 8 multiples of 10: 10 20 30 40 50 60 70 80
Enter a positive integer: 8
Test array: 1 1 2 3 5 8 13 
Counting up: 1 2 3 4 5 6 7 8 
Counting down: 8 7 6 5 4 3 2 1 
The first 8 multiples of 5: 13 13 13 13 13 13 13 13 
The first 8 multiples of 10: 18 18 18 18 18 18 18 18 
到目前为止,我的代码是这样的:

//Creates an array that counts up to the user input
public static int[] countUp(int n){
    int [] temp = new int[n];
    for(int i=0; i<n; i++){
        temp[i] = i+1;
    }
    return temp;
}

//Creates an array that counts down to the user input
public static int[] countDown(int n){
    int [] temp = new int[n];
    for(int i=0; i<temp.length; i++){
        temp[i] = n--;
    }
    return temp;
}

//Creates an array that gets n amount of multiples of 5
public static int[] getMultiplesOfFive(int n){
    int [] temp = new int[n];
    for(int i=0; i<temp.length; i++){
        temp[i] = n+5;
    }
    return temp;
}

//Creates an array that gets n amount of multiples of 10
public static int[] getMultiplesOfTen(int n){
    int [] temp = new int[n];
    for(int i=0; i<temp.length; i++){
        temp[i] = n+10;
    }
    return temp;
}
}
显然,问题出在最后两个名为getMultiplesofFive和getMultiplesofTen的数组中。我只是不知道如何创建一个循环来提供正确的输出。
非常感谢你

你的问题是你一直在计算相同的结果。将“n”传递到方法中,然后在数组的每个索引(i)中存储n+10的结果

您想要的是这样的(仅使用times 5示例):

int[]temp=newint[n];

对于(int i=1;i在乘法方法中,您没有进行乘法。请尝试以下方法:

    //Creates an array that gets n amount of multiples of 5
    public static int[] getMultiplesOfFive(int n){
        int [] temp = new int[n];
        for(int i=0; i<temp.length; i++){
            temp[i] = (i+1)*10;
        }
        return temp;
    }

    //Creates an array that gets n amount of multiples of 10
    public static int[] getMultiplesOfTen(int n){
        int [] temp = new int[n];
        for(int i=0; i<temp.length; i++){
            temp[i] = (i+1)*10;
        }
        return temp;
    }
//创建一个数组,该数组获取n个5的倍数
公共静态int[]GetMultipleOffive(int n){
int[]temp=新的int[n];

对于(int i=0;iYou也可以只执行int i=1;i True@AndreasBrunnet。但是,您仍然需要引用数组索引,在这种情况下减去1:
temp[i-1]=”。或者,您可以跳过i++增量,在数组索引中执行该操作:
for(int i=1;i //Creates an array that gets n amount of multiples of 5 public static int[] getMultiplesOfFive(int n){ int [] temp = new int[n]; for(int i=0; i<temp.length; i++){ temp[i] = (i+1)*10; } return temp; } //Creates an array that gets n amount of multiples of 10 public static int[] getMultiplesOfTen(int n){ int [] temp = new int[n]; for(int i=0; i<temp.length; i++){ temp[i] = (i+1)*10; } return temp; }