在while循环中返回一个字符串

时间:2014-11-18 14:23:50

标签: java string for-loop while-loop stringbuilder

在这里,我想避免使用行return "";,而是返回我用System.out.println();输出的所有值

我尝试过使用StringBuilder方法,但是在运行测试程序时没有输出任何内容。我猜测StringBuilder必须在while循环中以不同的方式工作,但我找不到如何!

任何建议或指导都将不胜感激!

public String toString(){
    int topLine = size;
    int topCurrentLine = size;
    int bottomLine = 1;
    int bottomCurrentLine = size;

    while(topCurrentLine<=size){
        topCurrentLine= (size-topLine)+1;

        if (topCurrentLine<=size){
            for (int i = 0; i < (topCurrentLine-1); ++i){
                System.out.print(".");
            }

            System.out.print(starMinusLine(getSandInLine(topLine), ((2*topLine-1)-getSandInLine(topLine)),false));

            for (int i = 0; i < (topCurrentLine-1); ++i){
                System.out.print(".");
            }
            topLine = topLine -1;

            System.out.print("\n");
        }       
    }

    while(bottomCurrentLine>1){         
        bottomCurrentLine= (size-bottomLine)+1;

        for (int i = 0; i < (bottomCurrentLine-1); ++i){
            System.out.print(".");
        }

        System.out.print(starMinusLine(getSandInLine(bottomLine), ((2*bottomLine-1)-getSandInLine(bottomLine))));

        for (int i = 0; i < (bottomCurrentLine-1); ++i){
            System.out.print(".");
        }
        bottomLine = bottomLine +1;

        System.out.print("\n");     
    }

    return "";      
}

以下是我对StringBuilder的尝试:

public String toString(){ 
    int topLine = size;
    int topCurrentLine = size;
    int bottomLine = 1;
    int bottomCurrentLine = size;

    StringBuilder y = new StringBuilder();

    while(topCurrentLine<=size) {
        topCurrentLine= (size-topLine)+1;

        if (topCurrentLine<=size){
            for (int i = 0; i < (topCurrentLine-1); ++i){
                y.append(".");
            }

            y.append(starMinusLine(getSandInLine(topLine), ((2*topLine-1)-getSandInLine(topLine)),false));

            for (int i = 0; i < (topCurrentLine-1); ++i){
                y.append(".");
            }
            topLine = topLine -1;

            y.append("\n");
        }

    }

    while(bottomCurrentLine > 1) {      
        bottomCurrentLine= (size-bottomLine)+1;

        for (int i = 0; i < (bottomCurrentLine-1); ++i){
            y.append(".");
        }

        y.append(starMinusLine(getSandInLine(bottomLine), ((2*bottomLine-1)-getSandInLine(bottomLine))));

        for (int i = 0; i < (bottomCurrentLine-1); ++i){
            y.append(".");
        }
        bottomLine = bottomLine +1;

        y.append("\n");
    }

    return y.toString();
}

3 个答案:

答案 0 :(得分:0)

StringBuilder可以完美地用于while循环。这是一个例子:

public String toString() {
    int size = 10;
    int current = 0;
    StringBuilder sb = new StringBuilder();
    sb.append("All values:");
    while (current <= size) {
        sb.append("\ncurrent = " + current);
        current++;
    }

    sb.toString();
}

此方法返回的字符串为:

All values:
current = 0
current = 1
current = 2
current = 3
current = 4
current = 5
current = 6
current = 7
current = 8
current = 9
current = 10

我希望这能回答你的问题。

答案 1 :(得分:0)

创建一个List对象并将其传递给你的函数toString(List yourlist)。 在你的toString(List yourlist)函数中,将你需要的字符串添加到yourList中。 您不需要返回列表。

答案 2 :(得分:0)

只需打印toString即可。就在

之前
return y.toString();

添加

System.out.println(y.toString);