Java程序不';I don’我不想从头开始,尽管我正试着告诉你

Java程序不';I don’我不想从头开始,尽管我正试着告诉你,java,loops,while-loop,Java,Loops,While Loop,基本上,我正在尝试使用main方法创建一个测试类。根据用户的输入,程序应该遵循一定的步骤顺序,然后在最后,我试图让程序重新从头开始(即,在程序开始时问第一个问题,而不必退出程序并重新启动) 我过去也做过类似的事情,我正试图用和以前一样的方式来做,但这次由于某种原因它不起作用 以下是我想做的基本要点: public class Payroll { public static void main(String[] args) { int steps = 0;

基本上,我正在尝试使用main方法创建一个测试类。根据用户的输入,程序应该遵循一定的步骤顺序,然后在最后,我试图让程序重新从头开始(即,在程序开始时问第一个问题,而不必退出程序并重新启动)

我过去也做过类似的事情,我正试图用和以前一样的方式来做,但这次由于某种原因它不起作用

以下是我想做的基本要点:

public class Payroll {
    public static void main(String[] args) {
        int steps = 0;

        while(steps == 0) {
        <Execute this code>
        steps = 1;
        }

        while(steps == 1) {
        <Execute this code>
        steps = 2;
        }

        while(steps == 2) {
        <Execute this code>
        steps = 0; //go back to the beginning
        }
    }
}
公共类工资单{
公共静态void main(字符串[]args){
int步数=0;
while(步骤==0){
步骤=1;
}
while(步骤==1){
步骤=2;
}
while(步骤==2){
steps=0;//返回到开头
}
}
}
问题是,当程序到达表示“steps=0”的步骤时,它将完全退出,而不是像我预期的那样返回到开始


有人知道我想让它做什么吗?

将三个while循环括在另一个while循环中,从
while(steps==0)
扩展到
while(steps==2)
的右括号
}
之后

如果希望
steps==2
循环控制流,则新的while循环可以具有条件
steps==0
。然后,您可以将steps设置为-1以退出封闭循环以及
steps==2
循环。像这样:

while(steps == 0) {
    while(steps == 0) { /* ... */ }
    while(steps == 1) { /* ... */ }
    while(steps == 2) { /* ... */ } // this loop sets steps back to 0 to keep
                                    // looping, or it sets steps to -1 to quit
}

显然,它不会从一开始就开始。您要做的是在三个独立的while循环中检查条件。如果其中三个失败,它应该退出。功能没有问题。正如@irrelephant所说,您可以将三个while循环封装在另一个while循环中

我建议在一个while循环中使用三种情况的开关。

您的代码与以下代码有何不同:

public static void main(String[] args) {
        <Execute step == 0 code>
        <Execute step == 1 code>
        <Execute step == 2 code>
}
publicstaticvoidmain(字符串[]args){
}
在这种情况下,它基本上不是:

public static void main(String[] args) {
        boolean done = false;

        while(!(done)) {
            <Execute step == 0 code>
            <Execute step == 1 code>
            <Execute step == 2 code>

            if(some condition) {
                done = true;
            }
        }
}
publicstaticvoidmain(字符串[]args){
布尔完成=假;
而(!(完成)){
如果(某些条件){
完成=正确;
}
}
}

但是嵌套循环是不可取的。尝试递归。使用循环,这就是我要做的。只要步骤=0,代码就会不断重复

public class Payroll {
    public static void main(String[] args) {
        int steps = 0;
        while (steps == 0) {

            while (steps == 0) {
                <Execute this code>
                steps = 1;

            }

            while (steps == 1) {
                <Execute this code>
                steps = 2;

            }

            while (steps == 2) {
                 <Execute this code>
                steps = 0; //go back to the beginning
            }
        }
    }
}
公共类工资单{
公共静态void main(字符串[]args){
int步数=0;
while(步骤==0){
while(步骤==0){
步骤=1;
}
while(步骤==1){
步骤=2;
}
while(步骤==2){
steps=0;//返回到开头
}
}
}
}

您是否考虑过对每个步骤使用单独的方法,并将它们链接在一起,如step0>-step1->step2->step0。我想那样看起来会干净得多way@user189最好先用if语句检查条件,然后按照您想做的顺序进行操作。更好的是,边做边做。我不确定您使用这段代码想要实现什么,但do while甚至更好。虽然这会起作用,但很奇怪……我更喜欢使用if条件的while循环,它更具可读性