Java将项目从INT数组添加到字符串数组
public static String fibonacci(int a, int b){
int max = 10;
String returnValue;
int[] result = new int[max];
result[0] = a;
result[1] = b;
for (int i1 = 2; i1 < max; i1++) {
result[i1] = result[i1 - 1] + result[i1 - 2];
}
for (int i3 = 0; i3 < max; i3++) {
//Here you can do something with all the values in the array one by one
//Maybe make something like this?:
int TheINTthatHasToBeAdded = result[i3];
//The line where TheINTthatHasToBeAdded gets added to the String returnValue
}
return returnValue;
}
-
-
结果数组的项是整数,返回值是字符串
我的问题是,;如何将结果数组中的项添加到returnValue数组中
# 1 楼答案
我想你是想返回一个字符串,其中包含你找到的所有fibonaci数字? 如果是,请更改以下内容:
将以下内容添加到第二个循环中
将返回值更改为:
这应该可以解决这个问题(最后加上一个“,”
# 2 楼答案
要将数组转换为} :
String
,可以使用^{尽管如此,返回计算数组的
String
表示仍然不是一个好的设计。最好返回int[]
,让客户端将其转换为String
或以其他方式使用它或向用户显示它以下是该方法的外观:
或者甚至使用另一种方式来消耗结果。下面是另一个例子: