java - 循环程序在完成检查后再次启动

标签 java loops exit automata

我正在尝试编写一个程序,用户在 a 和 b 上输入一个字符串,程序将检查它是否被接受。现在,如果用户输入“退出”,我设法使程序退出,但我无法弄清楚如何循环程序并使其在完成检查字符串时再次启动,要求用户再次输入。我该怎么做呢?下面是我的代码

import java.util.Scanner; 
public class Automata {
public static void main(String[] args) {
    Boolean keeprunning = true;
    Scanner inputScanner = new Scanner(System.in);
    System.out.println("Please enter your input");
    String input = inputScanner.next();
    char [] Inputted = input.toCharArray();

    if (input.equalsIgnoreCase("Exit")){
        keeprunning = false;
        System.out.println("Exit now");
    }

    String stateA = "A";
    String stateB = "B";
    String stateC = "C";
    String stateD = "D";
    String stateE = "E";
    String stateF = "F";
    String currentState = "A";

    while(keeprunning){
    for (int i = 0; i < Inputted.length; i++){
        if (Inputted[i]=='a' && currentState.equals(stateA)){
            currentState = "B";
            System.out.println(stateA + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i]=='b' && currentState.equals(stateA)){
            currentState = "F";
            System.out.println(stateA + " -- " + Inputted[i] + " --> " + stateF);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateB)){
            currentState = "B";
            System.out.println(stateB + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i] == 'b' && currentState.equals(stateB)){
            currentState = "C";
            System.out.println(stateB + " -- " + Inputted[i] + " --> " + stateC);
        } 

        else if (Inputted[i]== 'b' && currentState.equals(stateF)){
            currentState = "F";
            System.out.println(stateF + " -- " + Inputted[i] + " --> " + stateF);
        } else if (Inputted[i] == 'a' && currentState.equals(stateF)){
            currentState = "B";
            System.out.println(stateF + " -- " + Inputted[i] + " --> " + stateB);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateC)){
            currentState = "D";
            System.out.println(stateC + " -- " + Inputted[i] + " --> " + stateD);
        } else if (Inputted[i] == 'b' && currentState.equals(stateC)){
            currentState = "F";
            System.out.println(stateC + " -- " + Inputted[i] + " --> " + stateF);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateD)){
            currentState = "B";
            System.out.println(stateD + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i] == 'b' && currentState.equals(stateD)){
            currentState = "E";
            System.out.println(stateD + " -- " + Inputted[i] + " --> " + stateE);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateE)){
            currentState = "B";
            System.out.println(stateE + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i] == 'b' && currentState.equals(stateE)){
            currentState = "C";
            System.out.println(stateE + " -- " + Inputted[i] + " --> " + stateC);
        }   

    } if (currentState.equals(stateA) || currentState.equals(stateB) || currentState.equals(stateD) || currentState.equals(stateE)){
        System.out.println("Input accepted");
        keeprunning = false;
        break;
    } else if (currentState.equals(stateF) || currentState.equals(stateC)){
        System.out.println("Input not accepted");
        keeprunning = false;
        break;
    }

}
    }

感谢您的帮助:D

最佳答案

只需将 while 循环 header 移至顶部,就在 keeprunning 声明之后:

Boolean keeprunning = true;
while(keeprunning){
...

并从 if-else 语句中删除以下代码:

keeprunning = false;
break;

这应该可以解决您所说的问题

<小时/>
import java.util.Scanner; 
public class Automata {
public static void main(String[] args) {

Boolean keeprunning = true;
while(keeprunning){
    Scanner inputScanner = new Scanner(System.in);
    System.out.println("Please enter your input");
    String input = inputScanner.next();
    char [] Inputted = input.toCharArray();

    if (input.equalsIgnoreCase("Exit")){
        keeprunning = false;
        System.out.println("Exit now");
    }
    else{

    String stateA = "A";
    String stateB = "B";
    String stateC = "C";
    String stateD = "D";
    String stateE = "E";
    String stateF = "F";
    String currentState = "A";


    for (int i = 0; i < Inputted.length; i++){
        if (Inputted[i]=='a' && currentState.equals(stateA)){
            currentState = "B";
            System.out.println(stateA + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i]=='b' && currentState.equals(stateA)){
            currentState = "F";
            System.out.println(stateA + " -- " + Inputted[i] + " --> " + stateF);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateB)){
            currentState = "B";
            System.out.println(stateB + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i] == 'b' && currentState.equals(stateB)){
            currentState = "C";
            System.out.println(stateB + " -- " + Inputted[i] + " --> " + stateC);
        } 

        else if (Inputted[i]== 'b' && currentState.equals(stateF)){
            currentState = "F";
            System.out.println(stateF + " -- " + Inputted[i] + " --> " + stateF);
        } else if (Inputted[i] == 'a' && currentState.equals(stateF)){
            currentState = "B";
            System.out.println(stateF + " -- " + Inputted[i] + " --> " + stateB);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateC)){
            currentState = "D";
            System.out.println(stateC + " -- " + Inputted[i] + " --> " + stateD);
        } else if (Inputted[i] == 'b' && currentState.equals(stateC)){
            currentState = "F";
            System.out.println(stateC + " -- " + Inputted[i] + " --> " + stateF);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateD)){
            currentState = "B";
            System.out.println(stateD + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i] == 'b' && currentState.equals(stateD)){
            currentState = "E";
            System.out.println(stateD + " -- " + Inputted[i] + " --> " + stateE);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateE)){
            currentState = "B";
            System.out.println(stateE + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i] == 'b' && currentState.equals(stateE)){
            currentState = "C";
            System.out.println(stateE + " -- " + Inputted[i] + " --> " + stateC);
        }   

    } if (currentState.equals(stateA) || currentState.equals(stateB) || currentState.equals(stateD) || currentState.equals(stateE)){
        System.out.println("Input accepted");

    } else if (currentState.equals(stateF) || currentState.equals(stateC)){
        System.out.println("Input not accepted");
    }
}   

}
}
}

关于java - 循环程序在完成检查后再次启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22827230/

相关文章:

java - 意外的输入结束 : expected close marker

java - 如何关闭横向模式只是为了正常布局?

php - 带有 SimpleXMLElement 的大型 PHP for 循环非常慢 : memory issues?

java - 在循环中使用数组名称作为变量 (Java)

java - java中如何终止正在运行的循环?

Node.js 检测子进程退出

java - 以编程方式从关闭 Hook 访问退出状态

java - 来自非 Activity 类的处理程序

java - 循环输入用户输入

java - ImageIO 保存回原始大小