如何返回while循环java的开头

时间:2014-10-18 04:21:42

标签: java loops while-loop break

如何打破forloop并返回while循环的开头。当我输入1211时,我的函数应返回111221,因为有一个1,一个2和两个1。如果有人有任何想法,我真的很感激。

public static String occurence(String input) {

    int s = 0;
    String out = "";

    int count = 0;

    while(s < input.length()) {

        int num = getInt(input, s);

        for(int r = s; r < input.length(); r++) {

            if(num == getInt(input, r)) {
                count++;
            } else {
                System.out.println(count);

                out += count;
                out += num;
                s += count;
                count = 0;

                // Jump to start of while loop
            }
        }
    }
    return out;
}

1 个答案:

答案 0 :(得分:1)

使用break语句

 public static String occurence(String input){

int s = 0;
String out = "";

int count = 0;


while(s<input.length()){


    //System.out.println(s);

    int num = getInt(input, s);

    for(int r = s; r<input.length(); r++){


        if(num == getInt(input, r)){

            count++;


        }else{

            System.out.println(count);

            out+=count;
            out+=num;
            s+=count;
            count = 0;

            break;



        }

    }

}

return out;


}