2

我正在使用 for 循环,每次循环都需要返回一个字符串,但是使用“return”会中断循环,并且 Eclipse 会引发“无法访问代码”错误。有什么建议么

4

5 回答 5

2

如果返回ArrayList<String>, 不适合您的应用程序,这听起来像是回调函数的工作。定义回调接口:

public interface StringDelivery {
    public void processString(String aString);
}

然后在你的循环中你可以回调:

public void loopThroughStrings(StringDelivery callback) {
    for (. . .) {
        String nextString = . . .
        callback.processString(nextString);
    }
}

然后,您可以使用任何实现该接口的对象调用它。

编辑:

如果您正在计算一堆字符串但需要将它们作为单个字符串返回,那么您可以将它们放入一个数组中,然后用于Arrays.toString(Object[] array)将整个数组转换为单个字符串:

int n = <number of strings>
String[] strings = new String[n];
for (int i = 0; i < n; ++i) {
    strings[i] = <i-th string>
}
return Arrays.toString(strings);

返回值将使用由“,”分隔并括在方括号中的列表元素进行格式化:“[]”。

于 2012-09-28T03:40:19.920 回答
2

我认为你的逻辑被打破了

for (int counter = 0; counter < possibleAnswers.length; counter++){
    // This condition will be meet immediately because 0 is less then 25...
    if (counter < 25){
        return alpha[counter] + ": " + possibleAnswers[counter] + "\n";
    }
    // Meaning it is impossible for the program to ever reach this line...
    if (counter >= 26){
        return alpha[26] + a + ": " + possibleAnswers[counter] + "\n";
        a++;
    }
}

我认为你可能会更好地尝试类似...

StringBuilder sb = new StringBuilder(25);
for (int counter = 0; counter < possibleAnswers.length; counter++){
    if (counter < 25){
        sb.append(alpha[counter] + ": " + possibleAnswers[counter] + "\n");
    }
    if (counter >= 26){
        sb.append(alpha[26] + a + ": " + possibleAnswers[counter] + "\n");
        a++;
    }
}
return sb.toString();

更新工作示例

String possibleAnswers[] = new String[30];
String alpha[] = new String[30];

for (int index = 0; index < 30; index++) {
    possibleAnswers[index] = "Happy " + index;
    alpha[index] = Integer.toString(index);
}

int a = 0;

StringBuilder sb = new StringBuilder(25);
for (int counter = 0; counter < possibleAnswers.length; counter++) {
    if (counter < 25) {
        sb.append(alpha[counter]).append(": ").append(possibleAnswers[counter]).append("\n");
    }
    if (counter >= 26) {
        sb.append(alpha[26]).append(a).append(": ").append(possibleAnswers[counter]).append("\n");
        a++;
    }
}

System.out.println(sb);

哪个输出

0: Happy 0
1: Happy 1
2: Happy 2
3: Happy 3
4: Happy 4
5: Happy 5
6: Happy 6
7: Happy 7
8: Happy 8
9: Happy 9
10: Happy 10
11: Happy 11
12: Happy 12
13: Happy 13
14: Happy 14
15: Happy 15
16: Happy 16
17: Happy 17
18: Happy 18
19: Happy 19
20: Happy 20
21: Happy 21
22: Happy 22
23: Happy 23
24: Happy 24
260: Happy 26
261: Happy 27
262: Happy 28
263: Happy 29
于 2012-09-28T03:51:45.443 回答
0

您将需要使用 ArrayList 来存储所有字符串。

然后在 for 循环完成后返回 ArrayList。就像是。

String returnString = "";

for loop ....{
   returnString = returnString + " " + someNewString;
 }


return returnString
于 2012-09-28T03:38:12.740 回答
0

为什么要使事情复杂化..只需返回一个字符串数组

 public String[] function1()
 {
    String[] str1 new String(10);

    for(int i=0;i<10;i++)
    { 
          str1[i] ="string"+i;
    }
    return str1;
 }
于 2012-09-28T03:38:27.267 回答
0

您应该考虑从该方法返回一个字符串数组。

只是纠正一个 for 循环不会返回任何东西。它是返回值的函数。

最后,它很难评论,因为它不清楚你想要实现什么。

于 2012-09-28T03:38:41.973 回答