gpt4 book ai didi

java - 创建一个计数到给定数字的数组

转载 作者:行者123 更新时间:2023-11-30 10:47:59 25 4
gpt4 key购买 nike

我需要帮助来创建一个计数到给定数字的数组。输出应如下所示:

Enter a positive integer: 8
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

这是我目前所拥有的:

Scanner input = new Scanner(System.in);

int[] myList = new int[1];

System.out.print("Enter a positive integer: ");
promptUser(myList);

int[] testArray = { 1, 1, 2, 3, 5, 8, 13 };
System.out.print("Test array: ");
printArray(testArray);

System.out.print("Counting up: ");
int[] countingUp = countUp(n);
printArray(countingUp);
}

public static void promptUser(int[] a){
Scanner input = new Scanner(System.in);
for(int i=0; i<a.length; i++){
a[i] = input.nextInt();

}
}

public static void printArray(int[] array){
for(int i=0; i<array.length; i++)
System.out.print(array[i]);

}

public static int[] countUp(int n){
for(int i=0; i<n; i++){
int count = 0;
while(count<n){
count++;
}
}
}
}

除了最后一个名为 countingUp 的方法外,一切似乎都正常。

非常感谢!

最佳答案

   public static int[] countUp(int n){
for(int i=0; i<n; i++){
int count = 0;
while(count<n){
count++;
}
}
}

change this to

public static int[] countUp(int n){
int [] temp=new int[n];
for(int i=0; i<n; i++){
temp[i]=i+1;
}
return temp;
}



System.out.print("Counting up: ");
int[] countingUp = countUp(n);
printArray(countingUp);

In this line change to

int[] countingUp = countUp(n);
for(int i=0;i<countingUp.length;i++){
system.out.println(countingUp[i]+" ");
}

关于java - 创建一个计数到给定数字的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36002768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com