Java从被调用方法退出而while循环

时间:2018-08-28 10:33:29

标签: java

在s变量更新为0后,如何使此代码退出?到目前为止,它在停止之前执行了整个块的输出最后一个打印语句。这是while循环的正常行为吗?

public class test {

    private static int i = 0;
    private static int s = 1;
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        s:
        while(s>0 && i==0) {
            s();
        }


    }
    public static void s() {
        System.out.println(s);
        s--;
        //I want the program to stop here since s is already 0.
        System.out.println(s);
    }

}

1 个答案:

答案 0 :(得分:1)

尝试

public static void s() {
            System.out.println(s);
            s--;
            if(s!=0)
            {
                //I want the program to stop here since s is already 0.
                System.out.println(s);
            }
        }