如何将数组的每个元素打印到不同的行?
How to print each element of an array to a different line?
这就是我现在拥有的。我正在尝试将每个值打印到不同的行(要求 5)。
public class函数{
public static BufferedReader lifeIsBuff = new BufferedReader(new InputStreamReader(System.in));
public static void run() throws IOException {
FibonacciCalculator();
}
public static void FibonacciCalculator() throws IOException {
//1: Prompt the user for a positive number.
//2: Initialize an array to that size.
//3: Populate indices 0 and 1 with the value of 1.
//4: Using a for loop, populate the remaining spaces in the array with the correct Fibonacci value.
//5: Print each value to the console in their own lines.
//6: This app runs once and closes.
System.out.println("Please enter a positive number: ");
String input = lifeIsBuff.readLine();
int arraySize = Integer.parseInt(input);
long[] nums = new long[arraySize];
nums [0] = 1;
nums [1] = 1;
for (int i = 2; i < nums.length; i++) {
nums [i] = nums [i - 1] + nums [i - 2];
}
System.out.println(Arrays.toString(nums));
}
}
输出为:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
但是我在不同的线路上需要它们。
我尝试了各种方法,例如:
}
System.out.println(nums);
}
但这只是returns字符串:
[J@15db9742
我尝试过其他各种方法,但这是我得到的最接近的方法,并且我查找了各种方法将每个元素输出到它自己的行,但由于我是编码新手,所以我不能'无法理解它们或弄清楚如何让它们与我的代码一起工作。任何帮助,将不胜感激。
谢谢!
要在新行上打印元素,只需添加另一个 for 循环即可。
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
或
试试这个..
for (int i = 2; i < nums.length; i++) {
nums [i] = nums [i - 1] + nums [i - 2];
System.out.println(nums[i]);
}
这就是我现在拥有的。我正在尝试将每个值打印到不同的行(要求 5)。
public class函数{
public static BufferedReader lifeIsBuff = new BufferedReader(new InputStreamReader(System.in));
public static void run() throws IOException {
FibonacciCalculator();
}
public static void FibonacciCalculator() throws IOException {
//1: Prompt the user for a positive number.
//2: Initialize an array to that size.
//3: Populate indices 0 and 1 with the value of 1.
//4: Using a for loop, populate the remaining spaces in the array with the correct Fibonacci value.
//5: Print each value to the console in their own lines.
//6: This app runs once and closes.
System.out.println("Please enter a positive number: ");
String input = lifeIsBuff.readLine();
int arraySize = Integer.parseInt(input);
long[] nums = new long[arraySize];
nums [0] = 1;
nums [1] = 1;
for (int i = 2; i < nums.length; i++) {
nums [i] = nums [i - 1] + nums [i - 2];
}
System.out.println(Arrays.toString(nums));
}
}
输出为:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
但是我在不同的线路上需要它们。 我尝试了各种方法,例如:
}
System.out.println(nums);
}
但这只是returns字符串:
[J@15db9742
我尝试过其他各种方法,但这是我得到的最接近的方法,并且我查找了各种方法将每个元素输出到它自己的行,但由于我是编码新手,所以我不能'无法理解它们或弄清楚如何让它们与我的代码一起工作。任何帮助,将不胜感激。 谢谢!
要在新行上打印元素,只需添加另一个 for 循环即可。
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
或 试试这个..
for (int i = 2; i < nums.length; i++) {
nums [i] = nums [i - 1] + nums [i - 2];
System.out.println(nums[i]);
}